C# 和 Alpha Masking 中区域的抗锯齿

Anti-Aliasing for Regions in C# and Alpha Masking

我想在给定的图片上画一个圆环。 torus/ring 本身应该透明绘制。因为没有drawTorus,所以我使用图形路径得到结果。

到目前为止,这是我的代码:

Image bmp = Bitmap.FromFile(Application.StartupPath + "\background.jpg");
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;

Rectangle r1 = new Rectangle(bmp.Width / 2 - bmp.Height / 2, 0, bmp.Height, bmp.Height);
Rectangle r2 = Rectangle.Inflate(r1, -50, -50);

GraphicsPath p1 = new GraphicsPath();
p1.AddEllipse(r1);
GraphicsPath p2 = new GraphicsPath();
p1.AddEllipse(r2);

Region re = new Region(p1);
re.Xor(p2);

g.FillRegion(new SolidBrush(Color.FromArgb(80,0,0,0)),re);
g.Save();

结果如下所示:

问题是 SmoothingMode 被忽略了。你可以在这里看到它:

我了解到这是因为区域不受 SmoothingMode 参数的影响。

但是如何解决呢?

  1. 有没有其他方法可以在没有区域的情况下编写上面的代码?
  2. 我在 here 上读到了有关 AlphaMasking 的内容,但这也行不通,因为它也会影响环面的透明度。

使用 FillPath,您的问题应该可以解决。

GraphicsPath path = new GraphicsPath();
path.AddEllipse(r1);
path.AddEllipse(r2);

using(Brush b = new SolidBrush(Color.FromArgb(80,0,0,0)))
    g.FillPath(b, path);