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 1x1 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.


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds