问题描述
我已经加载到RAM中的24位BMP文件,我试图创建此映像文件HBITMAP。我已经发现了一些例子围绕我一直在尝试着,但似乎无法使工作。基本上,我需要一个HBITMAP的文件,这样我就可以卸载文件,只是不停,我可以用DeleteObject的后处理()的HBITMAP。由于此位是在我的应用程序加载很早,没有应用程序窗口,因此没有HDC。这是我到目前为止有: -
I have a 24bit BMP file loaded into RAM and I'm trying to create a HBITMAP for this image file. I have found some examples around which I've been experimenting with, but can't seem to make work. Basically, I need a HBITMAP for the file, so that I can unload the file and just keep the HBITMAP which I can dispose of later with DeleteObject(). Since this bitmap is loaded very early on in my application, there is no application Window and therefore no HDC. This is what I have so far:-
HBITMAP cBitmap; // This should be where my bitmap handle ends up.
mem; // This is a void* pointer to the loaded BMP file
tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)mem;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(mem + sizeof(tagBITMAPFILEHEADER));
RGBQUAD rgb = *(RGBQUAD*)(mem + sizeof(tagBITMAPFILEHEADER) + sizeof(tagBITMAPINFOHEADER));
BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;
UINT8* pixels = mem + bfh.bfOffBits;
void* ppv;
HBITMAP hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, &ppv, NULL, 0);
SetDIBits(NULL, hBitmap, 0, bih.biHeight, pixels, &bi, DIB_RGB_COLORS);
GetObject(hBitmap, sizeof(BITMAP), &cBitmap);
由于某种原因,只是令我感到困惑,关于CBitmap最终被NULL。这令我费解另一件事...... DIB_RGB_COLORS意味着BITMAPINFO有一个指针指向字面RGB值,但是这是如何工作的一个24bit的形象,这没有一个调色板?
For some reason that just baffles me, cBitmap ends up being NULL. And another thing that puzzles me... DIB_RGB_COLORS means that the BITMAPINFO has a pointer to literal RGB values, but how does that work with a 24bit image, which doesn't have a palette?
推荐答案
嗯,这不是多么 GetObject的
的作品。用法
Um, that's not how GetObject
works. The usage is
GetObject(handle, sizeof(object), &object);
在你的情况下,期望的是你怎么称呼它为
In your case, the expectation is that you call it as
BITMAP bitmap;
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
您不是一个指针传递给一个 BITMAP
结构,所以行为是不确定的。
You are not passing a pointer to a BITMAP
structure, so the behavior is undefined.
您不需要 GetObject的
可言。你已经拥有你的 HBITMAP
。这是在 HBITMAP
变量。
You don't need GetObject
at all. You already have your HBITMAP
. It's in the hBitmap
variable.
cBitmap = hBitmap;
这篇关于得到一个24位BMP文件HBITMAP加载到内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!