BOOL BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop) | |||||
HDC hdcDest; | /* handle of destination device context | */ | |||
int nXDest; | /* upper-left corner destination rectangle | */ | |||
int nYDest; | /* upper-left corner destination rectangle | */ | |||
int nWidth; | /* bitmap width, */ | ||||
int nHeight; | /* bitmap height, */ | ||||
HDC hdcSrc; | /* handle of source device context | */ | |||
int nXSrc; | /* upper-left corner source bitmap | */ | |||
int nYSrc; | /* upper-left corner source bitmap | */ | |||
DWORD dwRop; | /* raster operation for copy | */ |
The BitBlt function copies a bitmap from a specified device context to a destination device context.
hdcDest
Identifies the destination device context.
nXDest
Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.
nYDest
Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.
nWidth
Specifies the width, in logical units, of the destination rectangle and source bitmap.
nHeight
Specifies the height, in logical units, of the destination rectangle and source bitmap.
hdcSrc
Identifies the device context from which the bitmap will be copied. This parameter must be NULL if the dwRop parameter specifies a raster operation that does not include a source. This parameter can specify a memory device context.
nXSrc
Specifies the logical x-coordinate of the upper-left corner of the source bitmap.
nYSrc
Specifies the logical y-coordinate of the upper-left corner of the source bitmap.
dwRop
Specifies the raster operation to be performed. Raster operation codes define how the graphics device interface (GDI) combines colors in output operations that involve a current brush, a possible source bitmap, and a destination bitmap. This parameter can be one of the following:
Code | Description |
BLACKNESS | Turns all output black. |
DSTINVERT | Inverts the destination bitmap. |
MERGECOPY | Combines the pattern and the source bitmap by using the Boolean AND operator. |
MERGEPAINT | Combines the inverted source bitmap with the destination bitmap by using the Boolean OR operator. |
NOTSRCCOPY | Copies the inverted source bitmap to the destination. |
NOTSRCERASE | Inverts the result of combining the destination and source bitmaps by using the Boolean OR operator. |
PATCOPY | Copies the pattern to the destination bitmap. |
PATINVERT | Combines the destination bitmap with the pattern by using the Boolean XOR operator. |
PATPAINT | Combines the inverted source bitmap with the pattern by using the Boolean OR operator. Combines the result of this operation with the destination bitmap by using the Boolean OR operator. |
SRCAND | Combines pixels of the destination and source bitmaps by using the Boolean AND operator. |
SRCCOPY | Copies the source bitmap to the destination bitmap. |
SRCERASE | Inverts the destination bitmap and combines the result with the source bitmap by using the Boolean AND operator. |
SRCINVERT | Combines pixels of the destination and source bitmaps by using the Boolean XOR operator. |
SRCPAINT | Combines pixels of the destination and source bitmaps by using the Boolean OR operator. |
WHITENESS | Turns all output white. |
The return value is nonzero if the function is successful. Otherwise, it is zero.
An application that uses the BitBlt function to copy pixels from one window to another window or from a source rectangle in a window into a target rectangle in the same window should set the CS_BYTEALIGNWINDOW or CS_BYTEALIGNCLIENT flag when registering the window classes. By aligning the windows or client areas on byte boundaries, the application can ensure that the BitBlt operations occur on byte-aligned rectangles. BitBlt operations on byte-aligned rectangles are considerably faster than BitBlt operations on rectangles that are not byte-aligned.
GDI transforms the nWidth and nHeight parameters, once by using the destination device context, and once by using the source device context. If the resulting extents do not match, GDI uses the StretchBlt function to compress or stretch the source bitmap as necessary. If destination, source, and pattern bitmaps do not have the same color format, the BitBlt function converts the source and pattern bitmaps to match the destination. The foreground and background colors of the destination bitmap are used in the conversion.
When the BitBlt function converts a monochrome bitmap to color, it sets white bits (1) to the background color and black bits (0) to the foreground color. The foreground and background colors of the destination device context are used. To convert color to monochrome, BitBlt sets pixels that match the background color to white and sets all other pixels to black. BitBlt uses the foreground and background colors of the source (color) device context to convert from color to monochrome.
The foreground color is the current text color for the specified device context, and the background color is the current background color for the specified device context.
Not all devices support the BitBlt function. An application can determine whether a device supports BitBlt by calling the GetDeviceCaps function and specifying the RASTERCAPS index.
For a complete list of the raster-operation codes, see the Microsoft Windows Programmer's Reference, Volume 4.
The following example loads a bitmap, retrieves its dimensions, and displays it in a window:
HDC hdc, hdcMemory;
HBITMAP hbmpMyBitmap, hbmpOld;
BITMAP bm;
hbmpMyBitmap = LoadBitmap(hinst, "MyBitmap");
GetObject(hbmpMyBitmap, sizeof(BITMAP), &bm);
hdc = GetDC(hwnd);
hdcMemory = CreateCompatibleDC(hdc);
hbmpOld = SelectObject(hdcMemory, hbmpMyBitmap);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCCOPY);
SelectObject(hdcMemory, hbmpOld);
DeleteDC(hdcMemory);
ReleaseDC(hwnd, hdc);
GetDeviceCaps, PatBlt, SetTextColor, StretchBlt, StretchDIBits