Sunday, March 23, 2008

How do I load a bitmap object from a BMP file in memory?

Let's say you have a BMP file loaded into memory with handle file and you want to make a bitmap object that can be drawn to the device hdc. This function will do that for you.

HBITMAP LoadBitmapFromMemory(HDC hdc, const char *file)
{
HBITMAP ret;
BITMAPINFO *info;
BITMAPFILEHEADER *header;

header = (BITMAPFILEHEADER*)file;
if(header->bfType != 'MB') // code for bmp
return NULL;
info = (BITMAPINFO*)(file+sizeof(BITMAPFILEHEADER));
ret = CreateDIBitmap(hdc, &(info->bmiHeader), CBM_INIT,
file+header->bfOffBits, info, DIB_RGB_COLORS);
return ret;
}

No comments: