GDI +图像缩放问题

我有一个应用程序使用gdi +从一些位图绘制背景。 一些位图是垂直的非线性渐变(例如它们是1个像素宽,应该水平伸展以填充整个控制宽度)。 问题在于,对于小图像(如上所述),右侧的一些控制区域未被绘制。

我已经编写了一个测试程序,可以将1x1图像缩放到不同的大小,并且显示当缩放因子足够大时会发生问题

void Draw(HDC hDestDC, int destleft, int desttop, int destwidth, int destheight)
{
    COLORREF buffer[] = {0xFF0000FF }; // 1 pixel image
    const int width = 1, height = 1;
    Gdiplus::Bitmap gdipBitmap(width, height, 4*width, PixelFormat32bppARGB, (BYTE*)buffer);

    Gdiplus::ImageAttributes attrs;
    Gdiplus::Rect dest(destleft, desttop, destwidth, destheight);

    Gdiplus::Graphics graphics(hDestDC);
    graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
    graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);

    graphics.DrawImage(&gdipBitmap, dest, 0, 0, width, height, Gdiplus::UnitPixel, &attrs);
}

// OnPaint:
for(int i=0; i<800; ++i)
    Draw(hdc, 0, i, i, 1); // scale 1x1 image to different width

我会用这个测试来绘制一个平滑的“三角形”,但是线条的大小并不像目标矩形所指定的那么大:http://i.imgur.com/NNpMvmW.png

有没有办法解决这个问题? 我需要完全按照指定的尺寸输出位图(考虑源图像alpha通道和没有边缘背景的插值)。

PS:我必须使用GDI +,因为实际代码使用gdiplus提供的一些ImageAttributes。


好的,所以我最终尝试了不同选项的所有可能值,并最终找到适合我的值。

SetSmoothingMode (由Na Na建议)对图像没有任何视觉效果。

然而, SetInterpolationMode(InterpolationModeBilinear) (或更好)会生成精确的图像大小,但它也会插入图像边缘与背景。 结果看起来像这样,这不符合我的要求。

最后,设置ImageAttibuteWrapMode选项可以做到这一点:

attrs.SetWrapMode(Gdiplus::WrapModeTileFlipXY);

结果正是我想要的:http://i.imgur.com/rYKwAmZ.png

概要:

使用InterpolationModeNearestNeighbor 默认WrapMode导致渲染图像大小不准确。 为了避免这种情况,设置WrapModeTileFlipXY =>其他选项(如InterpolationMode )可以有任何所需的值,而不会影响渲染的图像大小。


你试过这个吗?

抗锯齿平滑方法可能会有所帮助。

链接地址: http://www.djcxy.com/p/50311.html

上一篇: GDI+ image scaling issue

下一篇: buffering *framework* in C and Windows GDI