c#将透明度设置为由polygon定义的区域

时间:2015-01-15 21:58:16

标签: c# graphics bitmap transparency polygon

我有一个位图,我想将透明度应用于由polygon定义的特定区域。那不是问题。我目前面临的问题是如何将透明度应用于多边形的INVERTED区域。我尝试过使用Bitmap.SetPixel(),但速度太慢了。这是我当前使用普通多边形的方法:

public Bitmap Mask(Bitmap b, Point[] mask, bool invert)
    {
        Bitmap ret = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(ret);
        g.Clear(Color.FromArgb(255,255,255,255));
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        g.DrawImage(b, 0, 0, width, height);
        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
        g.FillPolygon(new SolidBrush(Color.Transparent), mask);

        return ret;
    }

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。

我已将TextureBrushBitmap b as a texture一起使用,并在透明背景上绘制多边形,最终创建倒置多边形效果。

Bitmap ret = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(ret);
g.Clear(Color.FromArgb(0,255,255,255));
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

if (!invert)
{
    g.DrawImage(b, 0, 0, width, height);
    g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    g.FillPolygon(new SolidBrush(Color.Transparent), mask);
}
else g.FillPolygon(new TextureBrush(b), mask);
return ret;