Creating a Bitmap from a Framegrabber Image
-
Hello all, i'm trying to display a Bitmap in a Picture Control. The picture is captured by a CMOS camera (gray), and all i have is a pointer to the image in the Framegrabber buffer. The problem is that i don't have a real bitmap in that buffer, just an array with pixel values. The technical support of the Framgrabber producer couldn't help me till now and i'm still waiting for thier reply :sigh:. I was trying to create a Bitmap manualy and fill in all the needed structures like BITMAPINFO and so on but nothing worked. Can you tell me how to construct a bitmap from an pixelarray, just knowing the lenght, width and that's it is gray scale picture. I would be very greatfull for any kind of help! I wish you a nice Weekend :) im79
Gray as in 8-bits per pixel? Did you set up the color table? Show how you're calling CreateDIBitmap and your input values (BITMAPINFO).
-
Gray as in 8-bits per pixel? Did you set up the color table? Show how you're calling CreateDIBitmap and your input values (BITMAPINFO).
Hello all, hello hrfy and thanks for your answer :) Yes it is 8-bits per pixel. I didn't set the color table. I just called CreateBitmap or DIBitmap passing width, length, the pointer to the pixel array and so on. I as i explaind in my first posting, i don't have a BITMAPINFO, can make one manualy? :confused: I tried manythings but nothing worked. I'm new to windows programming and MFC. I'm not at work now, so i don't have the source code here right now. Eventhough i cann't provide enough info, i hope u can still help me on this one :~ Kind regards and thanks again , im79
-
Hello all, hello hrfy and thanks for your answer :) Yes it is 8-bits per pixel. I didn't set the color table. I just called CreateBitmap or DIBitmap passing width, length, the pointer to the pixel array and so on. I as i explaind in my first posting, i don't have a BITMAPINFO, can make one manualy? :confused: I tried manythings but nothing worked. I'm new to windows programming and MFC. I'm not at work now, so i don't have the source code here right now. Eventhough i cann't provide enough info, i hope u can still help me on this one :~ Kind regards and thanks again , im79
im79 wrote:
i don't have a BITMAPINFO, can make one manualy?
If you know what kind of data is in the pixel array, you should have no problem populating the header. I've just written and tested this by generating a grayscale gradient and it works.
HBITMAP CreateGrayscaleBitmap(int nWidth, int nHeight, LPBYTE pData)
{
BITMAPINFO *pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256)];
ZeroMemory(pbmi, sizeof(BITMAPINFO));
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = nWidth;
pbmi->bmiHeader.biHeight = -nHeight;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = nWidth * nHeight;RGBQUAD *pColors = pbmi->bmiColors;
for(int i = 0; i < 256; i++)
{
pColors->rgbBlue = i;
pColors->rgbGreen = i;
pColors->rgbRed = i;
pColors->rgbReserved = 0;
pColors++;
}CWindowDC dc(NULL);
HBITMAP hBitmap = CreateDIBitmap(dc, &pbmi->bmiHeader, CBM_INIT, pData, pbmi, DIB_RGB_COLORS);delete pbmi;
return hBitmap;
}This is for a top-down bitmap. If your image is flipped, remove the negative sign in front of the height.
-
im79 wrote:
i don't have a BITMAPINFO, can make one manualy?
If you know what kind of data is in the pixel array, you should have no problem populating the header. I've just written and tested this by generating a grayscale gradient and it works.
HBITMAP CreateGrayscaleBitmap(int nWidth, int nHeight, LPBYTE pData)
{
BITMAPINFO *pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256)];
ZeroMemory(pbmi, sizeof(BITMAPINFO));
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = nWidth;
pbmi->bmiHeader.biHeight = -nHeight;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = nWidth * nHeight;RGBQUAD *pColors = pbmi->bmiColors;
for(int i = 0; i < 256; i++)
{
pColors->rgbBlue = i;
pColors->rgbGreen = i;
pColors->rgbRed = i;
pColors->rgbReserved = 0;
pColors++;
}CWindowDC dc(NULL);
HBITMAP hBitmap = CreateDIBitmap(dc, &pbmi->bmiHeader, CBM_INIT, pData, pbmi, DIB_RGB_COLORS);delete pbmi;
return hBitmap;
}This is for a top-down bitmap. If your image is flipped, remove the negative sign in front of the height.
Hi, i've tried your code in my appicaltion and have another problem now :sigh:. I need a CBitmap object, so i tried the following:
CDC *pDC = GetDC(); CBitmap* myBitmap; myBitmap = CBitmap::FromHandle(hBitmap); myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog
I get an Unhandled exception in wingdi.cpp line 1102:BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); // <= line 1102 if (hObject == NULL) return FALSE; CHandleMap* pMap = afxMapHGDIOBJ(TRUE); ASSERT(pMap != NULL); pMap->SetPermanent(m_hObject = hObject, this); return TRUE; }
This is the call stack: >mfc71d.dll!CGdiObject::Attach(void * hObject=0x0305061d) Line 1102 + 0x1c C++ mfc71d.dll!CBitmap::CreateCompatibleBitmap(CDC * pDC=0x00816af4, int nWidth=400, int nHeight=400) Line 217 + 0x25 C++ I hope you can help me again here:) im79 -
Hi, i've tried your code in my appicaltion and have another problem now :sigh:. I need a CBitmap object, so i tried the following:
CDC *pDC = GetDC(); CBitmap* myBitmap; myBitmap = CBitmap::FromHandle(hBitmap); myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog
I get an Unhandled exception in wingdi.cpp line 1102:BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); // <= line 1102 if (hObject == NULL) return FALSE; CHandleMap* pMap = afxMapHGDIOBJ(TRUE); ASSERT(pMap != NULL); pMap->SetPermanent(m_hObject = hObject, this); return TRUE; }
This is the call stack: >mfc71d.dll!CGdiObject::Attach(void * hObject=0x0305061d) Line 1102 + 0x1c C++ mfc71d.dll!CBitmap::CreateCompatibleBitmap(CDC * pDC=0x00816af4, int nWidth=400, int nHeight=400) Line 217 + 0x25 C++ I hope you can help me again here:) im79im79 wrote:
myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight);
What are you trying to do here?
im79 wrote:
myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog
The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...
-
im79 wrote:
myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight);
What are you trying to do here?
im79 wrote:
myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog
The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...
hfry wrote:
im79 wrote: myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); What are you trying to do here?
I thought that i have to convert the DIB to a DDB Bitmap, so i guess that's wrong :~ I just want to display the Bitmap in a picture control, but it's not as easy as i thought
hfry wrote:
The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...
Yes that's stupid, I should have read the MSDN manuals
-
hfry wrote:
im79 wrote: myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); What are you trying to do here?
I thought that i have to convert the DIB to a DDB Bitmap, so i guess that's wrong :~ I just want to display the Bitmap in a picture control, but it's not as easy as i thought
hfry wrote:
The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...
Yes that's stupid, I should have read the MSDN manuals
im79 wrote:
it's not as easy as i thought
It is actually.
CStatic *picture = (CStatic*) GetDlgItem(IDC_STATIC_PICTURE);
picture->SetBitmap(hBitmap);Make sure the static control's type is actually set to Bitmap.
-
im79 wrote:
it's not as easy as i thought
It is actually.
CStatic *picture = (CStatic*) GetDlgItem(IDC_STATIC_PICTURE);
picture->SetBitmap(hBitmap);Make sure the static control's type is actually set to Bitmap.
-
im79 wrote:
i don't have a BITMAPINFO, can make one manualy?
If you know what kind of data is in the pixel array, you should have no problem populating the header. I've just written and tested this by generating a grayscale gradient and it works.
HBITMAP CreateGrayscaleBitmap(int nWidth, int nHeight, LPBYTE pData)
{
BITMAPINFO *pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256)];
ZeroMemory(pbmi, sizeof(BITMAPINFO));
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = nWidth;
pbmi->bmiHeader.biHeight = -nHeight;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = nWidth * nHeight;RGBQUAD *pColors = pbmi->bmiColors;
for(int i = 0; i < 256; i++)
{
pColors->rgbBlue = i;
pColors->rgbGreen = i;
pColors->rgbRed = i;
pColors->rgbReserved = 0;
pColors++;
}CWindowDC dc(NULL);
HBITMAP hBitmap = CreateDIBitmap(dc, &pbmi->bmiHeader, CBM_INIT, pData, pbmi, DIB_RGB_COLORS);delete pbmi;
return hBitmap;
}This is for a top-down bitmap. If your image is flipped, remove the negative sign in front of the height.
-
Hi :) i need your (or any one's) help again :) I'm trying for long time to change this code in order to make an 8Bit color Bitmap but it's not working :confused: I'll be greatfull for any help!
Hi,
RGBQUAD *pColors = pbmi->bmiColors;
for(int i = 0; i < 256; i++)
{
pColors->rgbBlue = i;
pColors->rgbGreen = i;
pColors->rgbRed = i;
pColors->rgbReserved = 0;
pColors++;
}The code snippet above is setting the color table. In your previous question, you wanted it grayscale so the color table so I set it up as such. If you want it to be color you need to setup the appropriate color table that your 8-bit data maps to. Regards, Justin Tay