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
- Device-Independent Bitmap (DIB): A bitmap format that describes pixel data in a way that is independent of the output device. This allows for portability and consistent rendering across different devices.
- Device-Dependent Bitmap (DDB): A bitmap format that is specific to the display device. These are often optimized for performance but are less portable.
- Bitmap Handles (HBITMAP): GDI objects that represent bitmaps in memory. These handles are used to refer to bitmaps in function calls.
- Color Formats: Bitmaps can have various color depths, such as 1-bit, 4-bit, 8-bit, 16-bit, 24-bit, and 32-bit per pixel.
Core Structures
The following structures are fundamental to working with bitmaps:
BITMAP: Contains information about a device-dependent bitmap (DDB).BITMAPINFO: Contains aBITMAPINFOHEADERstructure followed by an array ofRGBQUADstructures that specify the colors in the bitmap's color table. Used for DIBs.BITMAPINFOHEADER: Contains information about the dimensions and color format of a DIB.RGBQUAD: Defines a Red, Green, Blue, Alpha color quadruple.
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);
DeleteObject on GDI bitmap handles when they are no longer needed to prevent memory leaks.
Advanced Topics
- Using DIB sections for direct pixel manipulation.
- Image color transformations and effects.
- Loading and saving bitmap files (e.g., BMP, ICO).
- Using alpha blending for transparency.