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:

lpvBits [out]

A pointer to a buffer to receive the bitmap data. If this parameter is NULL, the function passes the dimensions and format of the bitmap to the BITMAPINFO structure pointed to by the lpbi parameter.

(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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top