GDI Bitmaps

This section covers the Windows Graphics Device Interface (GDI) functions and structures related to bitmap manipulation. Bitmaps are raster graphics, representing an image as a grid of pixels.

Key Concepts

Core Structures

The following structures are fundamental to working with bitmaps:

Core Functions

The following are some of the most commonly used GDI functions for bitmap operations:

Function Name Description
CreateBitmap Creates a DDB bitmap with the specified dimensions and color format.
CreateCompatibleBitmap Creates a DDB bitmap that is compatible with the specified device context (DC).
CreateBitmapIndirect Creates a DDB bitmap based on a pointer to a BITMAP structure.
CreateDIBSection Creates a DIB section, which is a DIB that can be directly written to by the application.
GetBitmap Retrieves information about a DDB bitmap and stores it in a BITMAP structure.
GetBitmapBits Retrieves the bitmap's pixel data.
SetBitmapBits Sets the pixel data for a bitmap.
DeleteObject Deletes a GDI bitmap object, freeing associated memory.
BitBlt Copies a rectangle of pixel data from one DC to another. Essential for drawing bitmaps.
StretchBlt Copies a rectangle of pixel data from one DC to another, stretching or compressing it to fit the destination rectangle.

CreateBitmap

HBITMAP CreateBitmap(int nWidth, int nHeight, int nPlanes, int nBitCount, const void *lpBits);

Creates a device-dependent bitmap (DDB) with the specified width, height, number of color planes, color depth, and optionally, initial pixel data.

// Example: Creating a 100x100 monochrome bitmap
HBITMAP hBitmap = CreateBitmap(100, 100, 1, 1, NULL);
if (hBitmap == NULL) {
    // Handle error
}

CreateCompatibleBitmap

HBITMAP CreateCompatibleBitmap(HDC hdc, int nWidth, int nHeight);

Creates a DDB bitmap that is compatible with the specified device context. This is useful for creating off-screen bitmaps that can be efficiently copied to the screen.

// Example: Creating a bitmap compatible with the screen DC
HDC hdcScreen = GetDC(NULL);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, 200, 150);
ReleaseDC(NULL, hdcScreen);
if (hBitmap == NULL) {
    // Handle error
}

BitBlt

BOOL BitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop);

Performs a bit-block transfer of the pixels from the source rectangle of the source device context to the destination rectangle of the destination device context. The dwRop parameter specifies how the source and destination bitmaps are combined.

// Example: Copying a bitmap from a memory DC to the screen DC
HDC hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hOldBitmap = SelectObject(hdcMem, hBitmap); // Select the bitmap into the memory DC

// Draw something on the bitmap in hdcMem if needed

BitBlt(hdcScreen, 10, 10, 100, 100, hdcMem, 0, 0, SRCCOPY); // Copy from memory to screen

SelectObject(hdcMem, hOldBitmap); // Restore the original bitmap
DeleteDC(hdcMem);
Note: Remember to always call DeleteObject on GDI bitmap handles when they are no longer needed to prevent memory leaks.

Advanced Topics