现在的位置: 首页 > 综合 > 正文

解决VC中图像缩小失真的问题:利用CDC::SetStretchBltMode

2013年05月26日 ⁄ 综合 ⁄ 共 2745字 ⁄ 字号 评论关闭

最近在处理图像放大缩小时,发现使用StretchBlt和StretchDIBitsStretchDIBits函数都有很大的失真问题存在。搜索MSDN(http://msdn.microsoft.com/zh-cn/library/s5a0ksxd(v=VS.80)),发现可以利用

CDC::SetStretchBltMode

函数解决。一般COLORONCOLOR模式典型地用于保留彩色位图中的颜色。

StretchBlt函数的位图拉伸模式。其原型如下:

int SetStretchBltMode(
   int nStretchMode );

nStrechMode可以选择下表中的值之一(HALFTONE):


使用消除和现在的像素颜色值进行逻辑与(and)操作运算。如果该位图是单色位图,那么该模式以牺牲白色像素为代价,保                                    留黑色像素点。

使用消除和现在的像素颜色值进行逻辑或(or)操作运算。
COLORONCOLOR:  删除像素。该模式删除所有消除的像素行,不保留其信息。

HALFTONE:              将源矩形区中的像素映射到目标矩形区的像素块中,覆盖目标像素块的一般颜色与源像素的颜色接近。在设置完
                             HALFTONE拉伸模之后,应用程序必须调用SetBrushOrgEx函数来设置刷子的起始点。如果没有成功,那么会出现刷子没对准的情况。


如果函数执行成功,那么返回值就是先前的拉伸模式STRETCH_ANDSCANSSTRETCH_DELETESCANS,
 STRETCH_ORSCANS中的一种
如果函数执行失败,那么返回值为0。

BLACKONWHITE(STRETCH_ANDSCANS)和WHITEONBLACK(STRETCH_ORSCANS)模式典型地用来保留单色位图中的前景像素。COLORONCOLOR(STRETCH_DELETESCANS)模式则典型地用于保留彩色位图中的颜色。

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 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/98: Same as BLACKONWHITE

STRETCH_DELETESCANS

Windows 95/98: Same as COLORONCOLOR

STRETCH_HALFTONE

Windows 95/98: Same as HALFTONE.

STRETCH_ORSCANS

Windows 95/98: 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.

The previous stretching mode. It can be STRETCH_ANDSCANSSTRETCH_DELETESCANS, or STRETCH_ORSCANS.

The bitmap-stretching mode defines how information is removed from bitmaps that are compressed by using the function.

The BLACKONWHITE (STRETCH_ANDSCANS) and WHITEONBLACK (STRETCH_ORSCANS) modes are typically used to preserve foreground pixels in monochrome bitmaps. The COLORONCOLOR (STRETCH_DELETESCANS)
mode is typically used to preserve color in color bitmaps.

The HALFTONE mode 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 that SetBrushOrgEx must be called
after setting the HALFTONE mode to avoid brush misalignment.

Additional stretching modes might also be available depending on the capabilities of the device driver.

抱歉!评论已关闭.