I want to use GetDIBits
to load a bitmap in C++. Here's the code I'm using:
HBITMAP hBmp = LoadBitmap(hInstance, MAKEINTRESOURCE(id));
BITMAP BM;
GetObject(hBmp, sizeof(BM), &BM);
GLvoid* bits = NULL;
BITMAPINFO bitmap_info;
memset(&bitmap_info, 0, sizeof(bitmap_info));
bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
bitmap_info.bmiHeader.biWidth = BM.bmWidth;
bitmap_info.bmiHeader.biHeight = BM.bmHeight;
bitmap_info.bmiHeader.biPlanes = 1;
bitmap_info.bmiHeader.biBitCount = DM_BITSPERPEL;//bits per pixel
bitmap_info.bmiHeader.biCompression = BI_RGB;
GetDIBits(device_context,
hBmp,
0, BM.bmWidth,
bits,
&bitmap_info,
DIB_RGB_COLORS);
But it seems that bits
is NULL
for some reason. Is there something wrong in my code? I used GetBitmapBits
before, bits
wasn't NULL
then.
The behaviour you are encountering is exactly as defined:
(Source: MSDN)
To summarize, you have to provide a non-zero pointer if you want
GetDIBits()
to fill in the bits. It is your responsibility to allocate the required memory.