11.3.1 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 must first create a memory device context and select the bitmap into it. The following example displays a bitmap by using BitBlt:

HDC hDC, hdcMemory;
    .
    .
    .

hDC = GetDC(hWnd);
if((hdcMemory = CreateCompatibleDC(hDC)) == NULL)
    return FALSE;


ReleaseDC(hWnd, hDC);
hOldBitmap = SelectObject(hdcMemory, hBitmap);

if (hOldbitmap) {
   BitBlt(hDC, 100, 30, 64, 32, hdcMemory, 0, 0, SRCCOPY);
   SelectObject(hdcMemory, hOldBitmap);
}

DeleteDC(hdcMemory);

In the example, 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 into the screen device context. The function places the upper-left corner of the bitmap at the coordinates (100,30), copying the entire bitmap, 64 bits wide by 32 bits high. The hDC and hdcMemory variables identify the destination and source contexts, respectively. The raster-operation code SRCCOPY directs BitBlt to copy the source bitmap without combining it with patterns or colors already at the destination.

The SelectObject, DeleteDC, and ReleaseDC functions clean up after the bitmap has been displayed. In general, when your application has finished using memory and device contexts, it should release them as soon as possible. Windows maintains a cache of five device contexts that are retrieved by the GetDC, GetWindowDC, and BeginPaint functions. If an application does not release one of these device contexts after using it, other applications might not be able to retrieve a context when one is needed. If you retrieve a device context by using GetDC, GetWindowDC, or BeginPaint, you must later release it by using ReleaseDC; if you create the device context by using any other function, you must later delete it by 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 given as 64 and 32 pixels, respectively. Another way to specify the width and height of the bitmap to be displayed is to retrieve these dimensions 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((HGDIOBJ) hBitmap, sizeof(BITMAP), &Bitmap);

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

BitBlt(hDC, 100, 30, Bitmap.bmWidth, Bitmap.bmHeight,
    hdcMemory, 0, 0, SRCCOPY);

The BitBlt function can display both monochrome and color bitmaps. No special steps are required to display bitmaps of different formats. Be aware, however, 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 screen, BitBlt converts the pixels having the current background color to white and all other pixels to black.