How to use Get/SetDIBits() like Get/SetBitmapBits()?
For now I'm just trying to copy one bitmap created with LoadImage() to another bitmap created with CreateCompatibleBitmap(). Problem is, the second bitmap ends up with all black pixels. Below is my code so far. I'm not sure about bi.biSizeImage, but I saw it in a few examples I found on MSDN.
Code:
hdc = CreateCompatibleDC(NULL);
bmp1 = (HBITMAP)LoadImage(NULL, L"bmp.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
bmp2 = CreateCompatibleBitmap(hdc, 256, 16);
BITMAP bmp;
GetObject(bmp1, sizeof(BITMAP), &bmp);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmp.bmWidth;
bi.biHeight = bmp.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) >> 3) * bi.biHeight;
char* bits = new char[bi.biSizeImage];
int ret1 = GetDIBits(hdc, bmp1, 0, bi.biHeight, bits, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
int ret2 = SetDIBits(hdc, bmp2, 0, bi.biHeight, bits, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
After that I select bmp2 into hdc and BitBlt it to a window but it's just a pure black bitmap. I was able to copy using GetBitmapBits() and SetBitmapBits() but I don't want to use those if they're deprecated.
Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?
Did you try to add LR_CREATEDIBSECTION flag to LoadImage?
Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?
Actually, I believe it is your first line. CreateCompatibleDC(NULL) will produce a monochrome DC until some object is selected into it. Instead, try CreateCompatibleDC(GetDC(hWnd)) where hWnd is a valid window handle.
See:
http://support.microsoft.com/kb/139165
Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?
I tried your guys' advice but still the same outcome :(
I found that the bits array contains only 0's and negative values. So I changed it to type unsigned char* and now it contains the correct pixel values. Still getting a black image in bmp2 however.
Re: How to use Get/SetDIBits() like Get/SetBitmapBits()?
I think you were right, hoxsiew. The problem was that I used hdc in the call to CreateCompatibleBitmap, thus making bmp2 a monochrome bitmap. I changed it to CreateCompatibleBitmap(GetDC(0), 256, 16) -- problem solved.
Thanks!