Suite101

Win32 Drawing and Bitmaps How-To

Windows Programming Tutorial for Graphics and Bitmap Handling

© Guy Lecky-Thompson

Jan 2, 2009
A Win32 graphics programming article detailing how to handle bitmaps and device contexts when using Windows for drawing and other graphical applications.

One issue that programmers face in writing graphics routines for the Win32 GDI API, is that it is not performance-oriented. While the rest of the Win32 GUI is more or less real-time, the graphics capabilities are anything but.

Luckily there is built-in support for off-screen drawing using bitmaps. Even if it is not possible in Win32 GDI programming to load a bitmap from a file, the standard GDI API does let the programmer create bitmaps for use in their own applications.

The CreateCompatibleBitmap Function

The way that the bitmap handling works is to base it on an existing device context. The CreateCompatibleBitmap function takes a handle to a device context, and then returns a bitmap of the specified dimensions that is compatible with it. It could be a handle to a screen, printer, plotter or braille device - the programmer does not need to worry about where the end result is going to end up.

The definition of the CreateCompatibleBitmap is as follows:

CreateCompatibleBitmap ( HDC, int, int );

The two integers are the width and height of the bitmap, and the HDC is a handle to an existing device context.

Getting an Appropriate Device Context

It is possible to use the device context that is currently selected. However, this would mean that, as the bitmap was built up, the user would be able to see the result, and it would be as slow, if not slower, than not using a bitmap at all.

While using the current device context is handy for taking a snapshot (the bitmap will capture the existing image), for offscreen drawing, it is necessary to create a separate device context:

HDC hOffscreenDC = CreateCompatibleDC(hdc); // hdc is current DC

Then, the bitmap can be created:

HBITMAP CreateCompatibleBitmap(hOffscreenDC, 640, 480); // assuming small area

To get the right dimensions in this step, the programmer should call GetClientRect to ascertain the available screen area, or calculate an appropriate amount : device contexts o not actually contain dimension information.

The BitBlt Function

With a bitmap, source, and target device contexts in hand, the programmer just needs to update the image, and then copy it from one device context (the offscreen DC) to the other (the visible DC). This is done with the BitBlt function:

BitBlt( HDC, int, int, int int, HDC, int, int, DWORD );

The above might look complicated, but essentially, the first HDC and associated integers is just the source image context and dimensions, and the second HDC is, predictably, the target context. The two integers associated with it is the X and Y position of the top corner of the bitmap : the size will be retained.

The final DWORD is the raster operation to use, which tells the Windows GDI API how to transfer the bitmap from one context to the other. Common values are:

  • SRCCOPY - copy the bitmap from one context to the other;
  • SRCPAINT - merge the source and destination using the OR operator;
  • SRCINVERT - merge the source and destination using the XOR operator.

Having dealt with the individual components, these can then be put in a simple series of steps.

Offscreen Drawing Tutorial

The following steps can be used to perform offscreen drawing:

  1. Create a compatible device context;
  2. Create a compatible bitmap;
  3. Draw on the bitmap using the new DC;
  4. Copy the bitmap using BitBlt;
  5. Blank the bitmap (if necessary);
  6. Repeat steps 3 to 6 until drawing finished;
  7. Delete the bitmap & DC.

As the reader will note from this Win32 graphics programming tutorial, it is very easy to handle bitmaps in Windows. While the speed will probably not be up to a full video game, it is ample for most non-performance critical applications. The technique is the same as for other graphical environments (like DirectX or OpenGL), but the implementation is different across SDKs.


The copyright of the article Win32 Drawing and Bitmaps How-To in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 Drawing and Bitmaps How-To in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo