Using the BitBlt Function to Display a Memory Bitmap

You can display any bitmap by using the BitBlt function. This function copies a bitmap from a source to a destination device context. To display a bitmap with BitBlt, you need to create a memory device context and select the bitmap into it first. The following example displays the bitmap by using BitBlt:

HDC hDC, hMemoryDC;

.

.

.

hDC = GetDC(hWnd);

hMemoryDC = CreateCompatibleDC(hDC);

hOldBitmap = SelectObject(hMemoryDC, hBitmap);

if(hOldbitmap)

{

BitBlt(hDC, 100, 30, 64, 32, hMemoryDC, 0, 0, SRCCOPY);

SelectObject(hMemoryDC, hOldBitmap);

}

DeleteDC(hMemoryDC);

ReleaseDC(hWnd, hDC);

The GetDC function specifies the device context for the client area of the window identified by the hWnd variable. The CreateCompatibleDC function creates a memory device context that is compatible with the device context. The SelectObject function selects the bitmap, identified by the hBitmap variable, into the memory device context and returns the previously selected bitmap. If SelectObject cannot select the bitmap, it returns zero.

The BitBlt function copies the bitmap from the memory device context to the screen device context. The function places the upper-left corner of the bitmap at the point (100, 30). The entire bitmap, 64 bits wide by 32 bits high, is copied. The hDC and hMemoryDC variables identify the destination and source contexts, respectively. The constant, SRCCOPY, is the raster-operation code. It directs BitBlt to copy the source bitmap without combining it with patterns or colors already at the destination.

Summary: Release device contexts as soon as you are done using them.

The SelectObject, DeleteDC, and ReleaseDC functions clean up after the bitmap has been displayed. In general, when you have finished using memory and device contexts, you should release them as soon as possible—especially device contexts, which are a limited resource. Windows maintains a cache of device contexts that all applications draw from. If an application does not release a device context after using it, other applications might not be able to retrieve a context when needed. If you get a device context by using GetDC, you must later release it using ReleaseDC; if you instead create the device context using CreateCompatibleDC, you must later delete it using DeleteDC. Before deleting a device context, you must call SelectObject, since you must not delete a device context while any bitmap other than the context's original bitmap is selected.

In the previous example, the width and height of the bitmap were assumed to be 64 and 32 pixels, respectively. Another way to specify the width and height of the bitmap to be displayed is to retrieve them from the bitmap itself. You can do this by using the GetObject function, which fills a specified structure with the dimensions of the given object. For example, to retrieve the width and height of a bitmap, you would use the following statements:

BITMAP Bitmap;

.

.

.

GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &Bitmap);

The next example copies the width and height of the bitmap to the bmWidth and bmHeight fields of the structure, Bitmap. You can use these values in BitBlt as follows:

BitBlt(hDC, 100, 30, Bitmap.bmWidth, Bitmap.bmHeight,

hMemoryDC, 0, 0, SRCCOPY);

The BitBlt function can display both monochrome and color bitmaps. No special steps are required to display bitmaps of different formats. However, you should be aware that BitBlt may convert the bitmap if its color format is not the same as that of the destination device. For example, when displaying a color bitmap on a monochrome display, BitBlt converts the pixels having the current background color to white and all other pixels to black.