绘制剪裁图像时可能会出现抗锯齿效果吗?

Uwe*_*eim 7 .net c# graphics system.drawing clipping

目前,我已成功使用Graphics该类绘制非矩形剪裁图像(内部龟):

在此输入图像描述

我的代码看起来像:

using (var g = Graphics.FromImage(image))
{
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;

    using (var gfxPath = new GraphicsPath())
    {
        gfxPath.AddEllipse(r);

        using (var region = new Region(r))
        {
            region.Exclude(gfxPath);

            g.ExcludeClip(region);

            g.DrawImage(turtleImage, r, r2, GraphicsUnit.Pixel);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这一切都按预期工作.我不知道如何解决的是使图像边框消除锯齿.

放大的图像看起来像:

在此输入图像描述

即图像结束的边界和图像的透明"背景"开始是粗略的,而不是平滑的α混合.

我的问题是:

是否可以剪切绘制的图像并使抗锯齿处于活动状态?

Tre*_*ott 6

如果你想要完整的羽毛,你应该考虑看一下这篇文章:

http://danbystrom.se/2008/08/24/soft-edged-images-in-gdi/

如果您想要一个快速简便的解决方案,您可以先绘制图像,然后使用带有抗锯齿的纯白色画笔在其上绘制一个GraphicsPath.你会做这样的事情:

Rectangle outerRect = ClientRectangle;
Rectangle rect = Rectangle.Inflate(outerRect, -20, -20);

using (Image img = new Bitmap("test.jpg"))
{
    g.DrawImage(img, outerRect);

    using (SolidBrush brush = new SolidBrush(Color.White))
    using (GraphicsPath path = new GraphicsPath())
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;

        path.AddEllipse(rect);
        path.AddRectangle(outerRect);

        g.FillPath(brush, path);
    }
}
Run Code Online (Sandbox Code Playgroud)