使用GetDIBits加载位图 [英] Using GetDIBits to load a bitmap
问题描述
我想使用 GetDIBits
在C ++中加载位图.这是我正在使用的代码:
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);
但是出于某种原因,似乎 bits
是 NULL
.我的代码有问题吗?我以前使用 GetBitmapBits
,然后 bits
并不是 NULL
.
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 [输出]
lpvBits [out]
指向缓冲区的指针,以接收位图数据.如果此参数是NULL,该函数将位图的尺寸和格式传递给lpbi参数指向的BITMAPINFO结构.
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.
(来源: MSDN)
总而言之,如果要 GetDIBits()
填充这些位,则必须提供一个非零的指针.您有责任分配所需的内存.
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.
这篇关于使用GetDIBits加载位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!