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);
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?