位图的拉伸显示

一,MSDN中的StretchBlt

标准解释

CDC::StretchBlt 
BOOL StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop );
Return Value
Nonzero if the bitmap is drawn; otherwise 0.
Parameters
x
Specifies the x-coordinate (in logical units) of the upper-left corner of the destination rectangle.
y
Specifies the y-coordinate (in logical units) of the upper-left corner of the destination rectangle.
nWidth
Specifies the width (in logical units) of the destination rectangle.
nHeight
Specifies the height (in logical units) of the destination rectangle.
pSrcDC
Specifies the source device context.
xSrc
Specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle.
ySrc
Specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle.
nSrcWidth
Specifies the width (in logical units) of the source rectangle.
nSrcHeight
Specifies the height (in logical units) of the source rectangle.
dwRop
Specifies the raster operation to be performed. Raster operation codes define how GDI combines colors in output operations that involve a current brush, a possible source bitmap, and a destination bitmap. This parameter may be one of the following values: 
BLACKNESS   Turns all output black.
DSTINVERT   Inverts the destination bitmap.
MERGECOPY   Combines the pattern and the source bitmap using the Boolean AND operator.
MERGEPAINT   Combines the inverted source bitmap with the destination bitmap 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 using the Boolean OR operator.
PATCOPY   Copies the pattern to the destination bitmap.
PATINVERT   Combines the destination bitmap with the pattern using the Boolean XOR operator.
PATPAINT   Combines the inverted source bitmap with the pattern using the Boolean OR operator. Combines the result of this operation with the destination bitmap using the Boolean OR operator.
SRCAND   Combines pixels of the destination and source bitmaps 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 using the Boolean AND operator.
SRCINVERT   Combines pixels of the destination and source bitmaps using the Boolean XOR operator.
SRCPAINT   Combines pixels of the destination and source bitmaps using the Boolean OR operator.
WHITENESS   Turns all output white. 

 百度百科的简要翻译: 
 

BOOL StretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, DWORD dwRop);
hdcDest:指向目标设备环境的句柄。
nXOriginDest:指定目标矩形左上角的X轴坐标,按逻辑单位表示坐标。
nYOriginDest:指定目标矩形左上角的Y轴坐标,按逻辑单位表示坐标。
nWidthDest:指定目标矩形的宽度,按逻辑单位表示宽度。
nHeightDest:指定目标矩形的高度,按逻辑单位表示高度。
hdcSrc:指向源设备环境的句柄。
nXOriginSrc:指向源矩形区域左上角的X轴坐标,按逻辑单位表示坐标。
nYOriginSrc:指向源矩形区域左上角的Y轴坐标,按逻辑单位表示坐标。
nWidthSrc:指定源矩形的宽度,按逻辑单位表示宽度。
nHeightSrc:指定源矩形的高度,按逻辑单位表示高度。
dwRop:指定要进行的光栅操作。光栅操作码定义了系统如何在输出操作中组合颜色,这些操作包括刷子、源位图和目标位图等对象。
二,拉伸模式设置SetStretchBltMode

CDC::SetStretchBltMode 
int SetStretchBltMode( int nStretchMode );
Return Value
The previous stretching mode. It can be STRETCH_ANDSCANS, STRETCH_DELETESCANS, or STRETCH_ORSCANS.
Parameters
nStretchMode
Specifies the stretching mode. It can be any of the following values:
Value Description 
BLACKONWHITE Performs a Boolean AND operation using the color values for the eliminated and existing pixels. If the bitmap is a monochrome bitmap, this mode preserves black pixels at the expense of white pixels. 
COLORONCOLOR Deletes the pixels. This mode deletes all eliminated lines of pixels without trying to preserve their information. 
HALFTONE Maps pixels from the source rectangle into blocks of pixels in the destination rectangle. The average color over the destination block of pixels approximates the color of the source pixels. 
 After setting the HALFTONE stretching mode, an application must call the Win32 function::SetBrushOrgEx to set the brush origin. If it fails to do so, brush misalignment occurs. 
STRETCH_ANDSCANS Windows 95: Same as BLACKONWHITE 
STRETCH_DELETESCANS Windows 95: Same as COLORONCOLOR 
STRETCH_HALFTONE Windows 95: Same as HALFTONE. 
STRETCH_ORSCANS Windows 95: Same as WHITEONBLACK 
WHITEONBLACK Performs a Boolean OR operation using the color values for the eliminated and existing pixels. If the bitmap is a monochrome bitmap, this mode preserves white pixels at the expense of black pixels. 
百度百科的简要翻译

函数原型:int SetStretchBltMode(HDC hdc, int iStretchMode);
参数:
hdc:设备环境句柄。
iStretchMode:指定拉伸模式。它可以取下列值,这些值的含义如下:
BLACKONWHITE:使用消除和现在的像素颜色值进行逻辑AND(与)操作运算。如果该位图是单色位图,那么该模式以牺牲白色像素为代价,保留黑色像素点。
COLORONCOLOR:删除像素。该模式删除所有消除的像素行,不保留其信息。
HALFTONE:将源矩形区中的像素映射到目标矩形区的像素块中,覆盖目标像素块的一般颜色与源像素的颜色接近。在设置完HALFTONE拉伸模之后,应用程序必须调用SetBrushOrgEx函数来设置刷子的起始点。如果没有成功,那么会出现刷子没对准的情况。
STRETCH_ANDSCANS:与BLACKONWHITE一样。
STRETCH_DELETESCANS:与COLORONCOLOR一样。
STRECH_HALFTONE:与HALFTONE相同。
STRETCH_ORSCANS:与WHITEONBLACK相同。
WHITEONBLACK:使用颜色值进行逻辑OR(或)操作,如果该位图为单色位图,那么该模式以牺牲黑色像素为代价,保留白色像素点。
返回值:如果函数执行成功,那么返回值就是先前的拉伸模式,如果函数执行失败,那么返回值为0。
Windows NT:若想获得更多错误信息,请调用GetLastError函数。
备注:拉伸模式在应用程序调用StretchBit函数时定义系统如何将位图的行或列与显示设备上的现有像素点进行组合。
BLACKONWHITE(STRETCH_ANDSCANS)和WHITEONBLACK(STRETCH_ORSCANS)模式典型地用来保留单色位图中的前景像素。COLORONCOLOR(STRETCH_DELETESCANS)模式则典型地用于保留彩色位图中的颜色。
HALFTONE模式比其他三种模式需要对源图像进行更多的处理,也比其他模式慢,但它能产生高质量图像,也应注意在设置HALFTONE模式之后,应调用SetBrushOrgEx函数以避免出现刷子没对准现象。
根据设备驱动程序的功能不同,其他一些拉伸模式也可能有效。

三,编程实例

这是本例的原图:

<MFC实践>位图的拉伸显示_第1张图片

1,创建一个单文档mfc工程

2,在视类中添加保护型成员变量

protected: 
BITMAP bmp;

3,在OnDraw函数中添加代码

void CStretchbltView::OnDraw(CDC* pDC)
{
	CStretchbltDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
 CBitmap cbmp;
 cbmp.LoadBitmap(IDB_BITMAP1);//加载位图
 CDC memdc;
 memdc.CreateCompatibleDC(pDC);
 memdc.SelectObject(&cbmp);    //选入对象
 cbmp.GetBitmap(&bmp);   
 //SetStretchBltMode(pDC->m_hDC, STRETCH_HALFTONE);//设置图像的拉伸方式
 pDC->StretchBlt(0,0,600,400,&memdc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);//显示位图
}
4,未设置拉伸方式的显示结果

(1)600x400的显示结果

<MFC实践>位图的拉伸显示_第2张图片

(2)400x400的显示结果

<MFC实践>位图的拉伸显示_第3张图片

(3)结论

原图型规格是1000x400的大小,很明显被压缩的越厉害,失真越厉害!但是当被拉伸显示时并未失真(不再演示)。

5,设置拉伸方式的显示结果(去掉SetStretchBltMode的注释//)

(1)600x400的显示结果

<MFC实践>位图的拉伸显示_第4张图片

(2)400x400的显示结果

<MFC实践>位图的拉伸显示_第5张图片



你可能感兴趣的:(位图的拉伸显示)