最近在处理图像放大缩小时,发现使用StretchBlt和StretchDIBitsStretchDIBits函数都有很大的失真问题存在。搜索MSDN(http://msdn.microsoft.com/zh-cn/library/s5a0ksxd(v=VS.80)),发现可以利用
CDC::SetStretchBltMode
函数解决。一般COLORONCOLOR模式典型地用于保留彩色位图中的颜色。
int SetStretchBltMode( int nStretchMode );
nStrechMode可以选择下表中的值之一(HALFTONE):
使用消除和现在的像素颜色值进行逻辑与(and)操作运算。如果该位图是单色位图,那么该模式以牺牲白色像素为代价,保 留黑色像素点。
使用消除和现在的像素颜色值进行逻辑或(or)操作运算。
COLORONCOLOR: 删除像素。该模式删除所有消除的像素行,不保留其信息。HALFTONE: 将源矩形区中的像素映射到目标矩形区的像素块中,覆盖目标像素块的一般颜色与源像素的颜色接近。在设置完 HALFTONE拉伸模之后,应用程序必须调用SetBrushOrgEx函数来设置刷子的起始点。如果没有成功,那么会出现刷子没对准的情况。
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 theHALFTONEstretching mode, an application must call the Win32 functionSetBrushOrgExto set the brush origin. If it fails to do so, brush misalignment occurs.
STRETCH_ANDSCANS
Windows 95/98: Same asBLACKONWHITE
STRETCH_DELETESCANS
Windows 95/98: Same asCOLORONCOLOR
STRETCH_HALFTONE
Windows 95/98: Same asHALFTONE.
STRETCH_ORSCANS
Windows 95/98: Same asWHITEONBLACK
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.
The bitmap-stretching mode defines how information is removed from bitmaps that are compressed by using the function.
TheBLACKONWHITE(STRETCH_ANDSCANS) andWHITEONBLACK(STRETCH_ORSCANS) modes are typically used to preserve foreground pixels in monochrome bitmaps. TheCOLORONCOLOR(STRETCH_DELETESCANS) mode is typically used to preserve color in color bitmaps.
TheHALFTONEmode requires more processing of the source image than the other three modes; it is slower than the others, but produces higher quality images. Also note thatSetBrushOrgExmust be called after setting theHALFTONEmode to avoid brush misalignment.
Additional stretching modes might also be available depending on the capabilities of the device driver.