Creating Color Bitmap From Scratch

Environment:VC6

How do you create a color bitmap from scratch? You need to be able to make the bitmap without .bmp file or resource. Using CreateBitmap or CreateBitmapIndirect creates monochrome bitmaps, unless you have HDC that can handle color bitmaps. However, creating HDC by CreateCompatibleDC(NULL) gives you simple HDC that contains 1×1 monochrome bitmap. Following is the source code shows how to create color bitmap from scratch using CreateDIBSection:


HDC dc= CreateCompatibleDC(NULL);
BITMAPINFO i;
ZeroMemory( &i.bmiHeader, sizeof(BITMAPINFOHEADER) );
i.bmiHeader.biWidth=5; // Set size you need
i.bmiHeader.biHeight=5; // Set size you need
i.bmiHeader.biPlanes=1;
i.bmiHeader.biBitCount=24; // Can be 8, 16, 32 bpp or
// other number

i.bmiHeader.biSizeImage=0;
i.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
i.bmiHeader.biClrUsed= 0;
i.bmiHeader.biClrImportant= 0;
VOID *pvBits;
HBITMAP hbmp= CreateDIBSection( dc,
&i,
DIB_RGB_COLORS,
&pvBits,
NULL,
0 );
// You don’t have to use ‘pvBits’, later DeleteObject(hbmp)
// will free this bit array.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read