Get image into DIB
-
Hi, Can anyone tell me how I can move an image into DIB? I hard coded my program to display images onto the screen correctly with 32bit/pixel, and now the program doesn't work when the screen is set to another color bit depth. Chris Becke's GDI tutorial was very helpful and led me to the GetDIBits and SetDIBits functions. I am planning to use GetDIBits(HDC, HBITMAP...) for converting my image to DIB, but I don't know what I should put in for HDC. The only context I can think of is my screenDC, but I doubt it is looking for my screenDC, rather, it's looking for the DC I originate my bitmap from... Is this correct? If so, then how can I create a DC for my image? My image is a string of BYTES with xBGRxBGRxBGRxBGR... where x is an unused byte. Thanks in advance for the help!:-O
-
Hi, Can anyone tell me how I can move an image into DIB? I hard coded my program to display images onto the screen correctly with 32bit/pixel, and now the program doesn't work when the screen is set to another color bit depth. Chris Becke's GDI tutorial was very helpful and led me to the GetDIBits and SetDIBits functions. I am planning to use GetDIBits(HDC, HBITMAP...) for converting my image to DIB, but I don't know what I should put in for HDC. The only context I can think of is my screenDC, but I doubt it is looking for my screenDC, rather, it's looking for the DC I originate my bitmap from... Is this correct? If so, then how can I create a DC for my image? My image is a string of BYTES with xBGRxBGRxBGRxBGR... where x is an unused byte. Thanks in advance for the help!:-O
try:
HBITMAP hDib = (HBITMAP)::CopyImage(bmp, IMAGE\_BITMAP, 0, 0, LR\_COPYRETURNORG | LR\_CREATEDIBSECTION);
'bmp' can be a DIB, DDB or CBitmap, the returned 'hDib' will be a DIB. --- It may be that your sole purpose in life is simply to serve as a warning to others.
-
try:
HBITMAP hDib = (HBITMAP)::CopyImage(bmp, IMAGE\_BITMAP, 0, 0, LR\_COPYRETURNORG | LR\_CREATEDIBSECTION);
'bmp' can be a DIB, DDB or CBitmap, the returned 'hDib' will be a DIB. --- It may be that your sole purpose in life is simply to serve as a warning to others.
Great! Thanks for the CopyImage tip. I'll give it a try. One more question, how does CopyImage know the bit depth of 'bmp'? Is there a flag in there where I set? This is how I am creating my bitmap:
CClientDC screenDC(pView); bitmapBefore.CreateCompatibleBitmap(&screenDC, sizeBefore.cx, sizeBefore.cy); bitmapBefore.SetBitmapBits(sizeBefore.cy*sizeBefore.cx*4, imageBuffer);
where imageBuffer is my Byte string with xBGRxBGRxBGR... Right now image only displays properly for 32bit/pixel displaying modes, and that's because I'm grabing DC from screen. Since the screen and my image are both 32bit/pixel, the image displays correctly. How do I make the bitmap have the information of 32bit/pixel when my screen DC is not? I have to replace CreateCompatibilityBitmap with something else don't I? Thanks again!:) -
Great! Thanks for the CopyImage tip. I'll give it a try. One more question, how does CopyImage know the bit depth of 'bmp'? Is there a flag in there where I set? This is how I am creating my bitmap:
CClientDC screenDC(pView); bitmapBefore.CreateCompatibleBitmap(&screenDC, sizeBefore.cx, sizeBefore.cy); bitmapBefore.SetBitmapBits(sizeBefore.cy*sizeBefore.cx*4, imageBuffer);
where imageBuffer is my Byte string with xBGRxBGRxBGR... Right now image only displays properly for 32bit/pixel displaying modes, and that's because I'm grabing DC from screen. Since the screen and my image are both 32bit/pixel, the image displays correctly. How do I make the bitmap have the information of 32bit/pixel when my screen DC is not? I have to replace CreateCompatibilityBitmap with something else don't I? Thanks again!:)Ok, if I understand you correctly, what you have is the dimensions of a bitmap (width and height), and an array of BYTEs specifying the data bits for a 32 bit bitmap, but you do not have the bitmap itself. You can create the bitmap using the CreateDIBitmap() or CreateDIBSection() functions. The HDC used just has to a HDC compatible with the screen.
HDC DC = ::CreateCompatibleDC(NULL);
BITMAPINFO bmi;
::ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitcount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = width * height * 4;void *dummy;
HBITMAP hDIB = ::CreateDIBSection(DC, &bmi, DIB_RGB_COLORS, &dummy, NULL, 0);
::SetDIBits(DC, hDIB, 0, height, Pointer_to_Data_Bits_Array, &bmi, DIB_RGB_COLORS);--- It may be that your sole purpose in life is simply to serve as a warning to others.
-
Ok, if I understand you correctly, what you have is the dimensions of a bitmap (width and height), and an array of BYTEs specifying the data bits for a 32 bit bitmap, but you do not have the bitmap itself. You can create the bitmap using the CreateDIBitmap() or CreateDIBSection() functions. The HDC used just has to a HDC compatible with the screen.
HDC DC = ::CreateCompatibleDC(NULL);
BITMAPINFO bmi;
::ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitcount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = width * height * 4;void *dummy;
HBITMAP hDIB = ::CreateDIBSection(DC, &bmi, DIB_RGB_COLORS, &dummy, NULL, 0);
::SetDIBits(DC, hDIB, 0, height, Pointer_to_Data_Bits_Array, &bmi, DIB_RGB_COLORS);--- It may be that your sole purpose in life is simply to serve as a warning to others.