StretchBlt
The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.
BOOL StretchBlt( HDC hdcDest, // handle to destination DC int nXOriginDest, // x-coord of destination upper-left corner int nYOriginDest, // y-coord of destination upper-left corner int nWidthDest, // width of destination rectangle int nHeightDest, // height of destination rectangle HDC hdcSrc, // handle to source DC int nXOriginSrc, // x-coord of source upper-left corner int nYOriginSrc, // y-coord of source upper-left corner int nWidthSrc, // width of source rectangle int nHeightSrc, // height of source rectangle DWORD dwRop // raster operation code );
Parameters
-
hdcDest
- [in] Handle to the destination device context. nXOriginDest
- [in] Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle. nYOriginDest
- [in] Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle. nWidthDest
- [in] Specifies the width, in logical units, of the destination rectangle. nHeightDest
- [in] Specifies the height, in logical units, of the destination rectangle. hdcSrc
- [in] Handle to the source device context. nXOriginSrc
- [in] Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle. nYOriginSrc
- [in] Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle. nWidthSrc
- [in] Specifies the width, in logical units, of the source rectangle. nHeightSrc
- [in] Specifies the height, in logical units, of the source rectangle. dwRop
-
[in] Specifies the raster operation to be performed. Raster operation codes define how the system combines colors in output operations that involve a brush, a source bitmap, and a destination bitmap.
See BitBlt for a list of common raster operation codes (ROPs). Note that the CAPTUREBLT ROP generally cannot be used for printing device contexts.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Windows NT/2000/XP: To get extended error information, call GetLastError.
Remarks
StretchBlt stretches or compresses the source bitmap in memory and then copies the result to the destination rectangle. This bitmap can be either a compatible bitmap (DDB) or the output from CreateDIBSection. The color data for pattern or destination pixels is merged after the stretching or compression occurs.
When an enhanced metafile is being recorded, an error occurs (and the function returns FALSE) if the source device context identifies an enhanced-metafile device context.
If the specified raster operation requires a brush, the system uses the brush currently selected into the destination device context.
The destination coordinates are transformed by using the transformation currently specified for the destination device context; the source coordinates are transformed by using the transformation currently specified for the source device context.
If the source transformation has a rotation or shear, an error occurs.
If destination, source, and pattern bitmaps do not have the same color format, StretchBlt converts the source and pattern bitmaps to match the destination bitmap.
If StretchBlt must convert a monochrome bitmap to a color bitmap, it sets white bits (1) to the background color and black bits (0) to the foreground color. To convert a color bitmap to a monochrome bitmap, it sets pixels that match the background color to white (1) and sets all other pixels to black (0). The foreground and background colors of the device context with color are used.
StretchBlt creates a mirror image of a bitmap if the signs of the nWidthSrc and nWidthDest parameters or if the nHeightSrc and nHeightDest parameters differ. If nWidthSrc and nWidthDest have different signs, the function creates a mirror image of the bitmap along the x-axis. If nHeightSrc and nHeightDest have different signs, the function creates a mirror image of the bitmap along the y-axis.
Not all devices support the StretchBlt function. For more information, see the GetDeviceCaps.
ICM: No color management is performed when a blit operation occurs.
Windows 98/Me, Windows 2000/XP: When used in a multiple monitor system, both hdcSrc and hdcDest must refer to the same device or the function will fail. To transfer data between DCs for different devices, convert the memory bitmap to a DIB by calling GetDIBits. To display the DIB to the second device, call SetDIBits or StretchDIBits.
Example Code
For an example, see Scaling an Image.
Requirements
Windows NT/2000/XP/Vista: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Use Gdi32.lib.
See Also
Bitmaps Overview, Bitmap Functions, BitBlt, CreateDIBSection, GetDeviceCaps, GetDIBits, MaskBlt, PlgBlt, SetDIBits, SetStretchBltMode , StretchDIBits
Scaling an Image
Some applications scale images that is, they display zoomed or reduced views of an image. For example, a drawing application may provide a zoom feature that enables the user to view and edit a drawing on a pixel-by-pixel basis.
Applications scale images by calling the StretchBlt function. Like the BitBlt function, StretchBlt copies bitmap data from a bitmap in a source DC into a bitmap in a target DC. However, unlike the BitBlt function, StretchBlt scales the image based on the specified dimensions of the source and target rectangles. If the source rectangle is larger than the target rectangle, the resultant image will appear to have shrunk; if the source rectangle is smaller than the target rectangle, the resultant image will appear to have expanded.
If the target rectangle is smaller than the source rectangle, StretchBlt removes color data from the image according to a specified stretch mode as shown in the following table.
Stretch mode | Method |
---|---|
BLACKONWHITE | Performs a logical AND operation on the color data for the eliminated pixels and the color data for the remaining pixels. |
WHITEONBLACK | Performs a logical OR operation on the color data for the eliminated pixels and the color data for the remaining pixels. |
COLORONCOLOR | Eliminates the color data of the deleted pixels completely. |
HALFTONE | Approximates the original (source) color data in the destination. |
You set the stretch mode by calling the SetStretchBltMode function.
The following example code is taken from an application that displays an image either at its original size or at twice its original size. (This application uses the default stretch mode.)
hdcScaled = CreateCompatibleDC(hdcScreen); hbmScaled = CreateCompatibleBitmap(hdcScreen, GetDeviceCaps(hdcScreen, HORZRES) * 2, GetDeviceCaps(hdcScreen, VERTRES) * 2); if (hbmScaled == 0) errhandler("hbmScaled", hwnd); // Select the bitmaps into the compatible DC. if (!SelectObject(hdcScaled, hbmScaled)) errhandler("Scaled Bitmap Selection", hwnd); case WM_COMMAND: // message: command from application menu switch(wParam) { case IDM_SCALEX1: if (fBlt) { fScaled = FALSE; hdcWin = GetDC(hwnd); BitBlt(hdcWin, 0,0, bmp.bmWidth, bmp.bmHeight, hdcCompatible, 0,0, SRCCOPY); ReleaseDC(hwnd, hdcWin); } break; case IDM_SCALEX2: if (fBlt) { fScaled = TRUE; StretchBlt(hdcScaled, 0, 0, bmp.bmWidth * 2, bmp.bmHeight * 2, hdcCompatible, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY); hdcWin = GetDC(hwnd); BitBlt(hdcWin, 0,0, bmp.bmWidth, bmp.bmHeight, hdcScaled, 0,0, SRCCOPY); ReleaseDC(hwnd, hdcWin); } break;
设备上下文绘图有很多种方法。例如通过创建位图画刷,利用其填充一个区域来实现图像的绘制。此外,还可以使用CDC类的位图函数来输出位图到设备上下文中。
BitBlt 用于从原设备中复制位图到目标设备,语法格式如下:
BOOLBitBlt(int x,int y,int nWidth,int nHeight,CDC*pSrcDC,int xSrc,int ySrc,DWORDdwRop);
x:目标矩形区域的左上角x轴坐标点。
y:目标矩形区域的左上角y轴坐标点。
nWidth:在目标设备中绘制位图的宽度。
nHight:在目标设备中绘制位图的高度。
pSrcDC:源设备上下文对象指针。
xSrc:源设备上下文的起点x轴坐标,函数从该起点复制位图到目标设备。
ySrc:源设备上下文的起点y轴坐标,函数从该起点复制位图到目标设备。
dwRop:光栅操作代码
BLACKNESS 使用黑色填充目标区域 DSTINVERT 目标矩阵区域颜色取反 MERGECOPY 使用与运算组合原设备矩形区域的颜色和目标设备的画刷 MERGEPAINT 使用或运算将反向的源矩形区域的颜色和目标矩形区域的颜色合并 NOTSRCCOPY 复制源设备区域的反色到目标设备中 NOTSRCERASE 使用或运算组合源设备区域与目标设备区域的颜色,然后对结果颜色取反 PATCOPY 复制源设备当前选中的画刷到目标设备 PATINVERT 使用异或运算组合目标设备选中的画刷和目标设备区域的颜色 PATPAINT 通过或运算组合目标区域当前选中的画刷和源设备区域反转的颜色 SRCAND 使用与运算组合源设备和目标设备区域的颜色 SRCCOPY 直接复制源设备区域到目标设备中 SRCERASE 使用与运算组合目标设备区域的反色与源设备区域的颜色 SRCINVERT 使用异或运算组合源设备区域颜色和目标设备区域颜色 SRCPAINT 使用或运算组合源设备区域颜色和目标设备区域颜色 WHITENESS 使用白色填充目标区域 |
BOOLStrevhBlt(int x,int y,int nWidth,int nHeight,CDC* pSrcDC,int xSrc,int ySrc,intnSrcWidth,int nSrcHeight,DWORD dwRop);
x:目标矩形区域的左上角x轴坐标点。
y:目标矩形区域的左上角y轴坐标点。
nWidth:在目标设备中绘制位图的宽度。
nHight:在目标设备中绘制位图的高度。
pSrcDC:源设备上下文对象指针。
xSrc:源设备上下文的起点x轴坐标,函数从该起点复制位图到目标设备。
ySrc:源设备上下文的起点y轴坐标,函数从该起点复制位图到目标设备。
nSrcWidth;需要复制的位图宽度。
nSrcHeight;需要复制的位图高度。
dwRop:光栅操作代码。
下面是两种方法对比程序,程序代码如下:
void CoutoutBmpView::OnDraw(CDC* pDC)
{
COutputBmpDoc* pDoc =GetDocument();//获取文档对象
ASSERT_VALID(pDoc);//验证文档对象
CDC memDC;//定义一个设备上下文
memDC.CreateCompatibleDC(pDC);//创建兼容的设备上下文
CBitmap bmp;//定义位图对象
bmp.LoadBitmap(IDB_BKBITMAP);//加载位图
memDC.SelectObject(&bmp);//选中位图对象
pDC->BitBlt(30,20,180,180,&memDC,1,1,SRCCOPY);//绘制位图
//以上是利用BitBlt来绘制位图
CRect rc(30,20,210,200);//定义一个区域
CBrush brush(RGB(0,0,0));//定义一个黑色的画刷
pDC->FrameRect(rc,&brush);//绘制矩形边框
//只是用来观察两者区别的,绘制一个矩形框
rc.OffsetRect(220,0);//移动区域
//下边是利用StretchBlt绘制的位图
BITMAP BitInfo;//定义位图结构
bmp.GetBitmap(&BitInfo);//获取位图信息
int x = BitInfo.bmWidth;//获取位图宽度
int y = BitInfo.bmHeight;获取位图高度
pDC->StretchBlt(rc.left,rc.top,rc.Width(),rc.Height(),&memDC,0,0,x,y,SRCCOPY);//绘制位图
pDC->FrameRect(rc,&brush);//绘制边框
brush.DeleteObject();//释放画刷
memDC.DeleteDC();//释放设备上下文
bmp.DeleteObject();//释放位图对象
}
- 上一篇:settimer、ontimer用法
- 下一篇:打开窗口固定大小