63.1.1 Bitmaps, Drawing Surfaces, and Device Contexts

A bitmap is one of seven objects that can be selected into a device context. The other six are the pen, the brush, the font, the region, the logical palette, and the drawing surface. (For more information about device contexts and the related objects, see Chapter 62, “Device Contexts.”)

63.1.1.1 The Drawing Surface

Once you create a device context by calling CreateDC, or obtain one of the Window Manager's device contexts by calling GetDC, you obtain a handle that identifies a device context. Prior to returning this handle, the Window Manager selects a drawing surface into the DC. If you called the CreateDC function to create a device context for a VGA display, the dimensions of this drawing surface are 640 by 480 pixels. If you call the GetDC function, the dimensions of this bitmap reflect the size of the client area.

When you pass the handle returned by CreateDC or GetDC to one of the GDI drawing functions, the output which you requested will appear on the drawing surface that is selected into the device context.

Compatible Device Contexts

Windows provides a special drawing surface for bitmap operations called a compatible DC. A compatible DC is an array of bits in memory that you can use to temporarily store the color data for bitmaps that were created on a normal drawing surface. You create a compatible DC by calling the CreateCompatibleDC function and passing a handle which identifies a normal device context. When you create a compatible device context, Windows creates a temporary placeholder for the array that is one-bit in length. Before performing any drawing operations using the compatible DC handle, you must increase the size of this array. To do this, you should call the CreateBitmap, CreateBitmapIndirect or CreateCompatibleBitmap function to create a bitmap of the appropriate dimensions and then call the SelectObject function to select the bitmap into the DC. Upon selecting the bitmap into the compatible DC, Windows replaces the single bit array with an array that is large enough to store color information for the specified rectangle of pixels.

When you pass the handle returned by CreateCompatibleDC to one of the GDI drawing functions, the output which you requested will not appear on a device's drawing surface—instead Windows stores the color information for the resultant line, curve, text or region in the array of bits. (You can copy the image stored in memory back onto a drawing surface by calling the BitBlt function, identifying the compatible DC as the source device context and identifying a window or screen DC as the target device context.)