The usage of BitBlt in C++

  • 2020-05-30 20:46:15
  • OfStack

BitBlt

This function performs a bitblock (bit_block) conversion of pixels in the specified source device environment area to the target device environment.

Prototype:


BOOL BitBlt(

  HDC hdcDest,

  int nXDest, int nYDest, int nWidth, int nHeight,

  HDC hdcSrc,

  int nXSrc, int nYSrc,

  DWORD dwRop) ; 

Parameters:

hdcDest: handle to the target device environment.

nXDest, nYDest: specifies the logical coordinates of the X axis and Y axis in the upper left corner of the destination rectangle

nWidth, nHeight: specifies the logical width and logical height of the source and destination rectangles.

hdcSrc: handle to the source device environment.

nXSrc, nYSrc: specifies the logical coordinates of the X and Y axes in the upper left corner of the source rectangle area.

dwRop: specifies the raster operation code. This code defines the color data for the source rectangle region and how it is combined with the color data for the destination rectangle region to achieve the final color. Here are some common raster operation codes:

描述

BLACKNESS

表示使用与物理调色板的索引0相关的色彩来填充目标矩形区域,(对缺省的物理调色板而言,该颜色为黑色)。

DSTINVERT

表示使目标矩形区域颜色取反。

MERGECOPY 

表示使用布尔型AND(与)操作符将源矩形区域的颜色与特定模式组合1起。

MERGEPAINT

通过使用布尔型OR(或)操作符将源矩形区域的颜色取反后与目标矩形区域的颜色合并。

NOTSRCCOPY

将源矩形区域颜色取反,拷贝到目标矩形区域。

NOTSRCERASE

使用布尔类型的OR(或)操作符组合源和目标矩形区域的颜色值,然后将合成的颜色取反。

PATCOPY

将特定的模式拷贝到目标位图上。

PATINVERT

通过使用布尔型XOR(异或)操作符将源和目标矩形区域内的颜色合并。

PATPAINT

通过使用布尔型OR(或)操作符将源矩形区域取反后的颜色值与特定模式的颜色合并。然后使用OR(或)操作符将该操作的结果与目标矩形区域内的颜色合并。

SRCAND

通过使用布尔型AND(与)操作符来将源和目标矩形区域内的颜色合并。

SRCCOPY

将源矩形区域直接拷贝到目标矩形区域。

SRCERASE

通过使用布尔型AND(与)操作符将目标矩形区域颜色取反后与源矩形区域的颜色值合并。

SRCINVERT

通过使用布尔型XOR(异或)操作符将源和目标矩形区域的颜色合并。

SRCPAINT

通过使用布尔型OR(或)操作符将源和目标矩形区域的颜色合并。

WHITENESS

使用与物理调色板中索引1有关的颜色填充目标矩形区域。(对于缺省物理调色板来说,这个颜色就是白色)。

For the complete raster operation (ROP) code, see Ternary Raster Operations.

The return value:

If the function succeeds, the return value is non-zero. If the function fails, the return value is zero. Call the GetLastError function to get the extension error.

Description:

If the rotation or shear transformation can be performed in the source device environment, the function BitBlt returns an error.

If there are other transformations (and matching transformations in the target device environment are invalid), then the rectangular area in the target device environment will be stretched, compressed, or rotated as needed.

If the color format of the source and target device environments does not match, the BitBlt function converts the color format of the source scene to a format that matches the target format.

An error occurs if the source device environment is identified as an enhanced metafile device environment while recording an enhanced metafile.

Not all devices support the BitBlt function. For more information, call the GetDeviceCaps function and assign the second parameter to RC_BITBLT to see if the device supports it. (For more information, see the BITBLT raster entry in function, as well as the and StretchBlt StretchBlt StretchBlt functions.)

If the source and target device environments represent different devices, the BitBlt function returns an error.

For more information about transferring display information from right to left to bit blocks (For information about blitting to displays with right-to-left orientations), see Creating Bitmaps Bitmaps.

In Windows CE 1.0 and 1.01, the parameter dwRop can only be specified with the following values: SRCCOPY, SRCAND, SRCPAINT, SRCINVERT. In Windows CE 2.0 and later, the parameter dwRop can be any ROP3 code value.

Here's an example from MSDN:


HBITMAP CopyBitmap( HBITMAP hbm) {
  HDC hdcSrc = CreateCompatibleDC(NULL);
  HDC hdcDst = CreateCompatibleDC(NULL);
  HBITMAP hbmOld, hbmOld2, hbmNew;
  BITMAP bm;
  GetObject(hbm, sizeof(bm), &bm);
  hbmOld = SelectObject(hdcSrc, hbm);
  hbmNew = CreateBitmap( bm.bmWidth, bm.bmHeight, bm.bmPlanes,
    bm.bmBitsPixel,
    NULL);
  hbmOld2 = SelectObject(hdcDst, hbmNew);
  BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
  SelectObject(hdcSrc, hbmOld);
  SelectObject(hdcDst, hbmOld2);
  DeleteDC(hdcSrc);
  DeleteDC(hdcDst);
  return hbmNew;
} 

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: