系统绘图反转区域构建路径

时间:2012-10-23 21:37:02

标签: c# winforms drawing

我有一个panel并且我在panel上画了一颗心。 但是我不想画出心脏,除了心脏之外我想绘制所有东西,所以心脏是透明的。 我可以反转从Region中选择的Path吗?

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
            path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
            path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
            this.Region = new Region(path);
            this.BackColor = Color.Black;

它看起来像什么(白色=透明):1

我希望它看起来像什么(白色=透明): enter image description here

4 个答案:

答案 0 :(得分:4)

我认为你可以一起添加2个图形路径。

您可以尝试使用此代码:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath path = new GraphicsPath();
    path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
    path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
    path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);

    GraphicsPath path2 = new GraphicsPath();
    path2.AddRectangle(new Rectangle(new Point(0, 0), panel1.Size));

    path2.AddPath(path, false);

    e.Graphics.FillPath(Brushes.Black, path2);
}

结果是:

Drawing the black heart on top of a background image

答案 1 :(得分:2)

您可以尝试从面板的Paint事件的Graphic对象中排除某个区域:

GraphicsPath path = new GraphicsPath();
path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);

using (Region r = new Region(path)) {
  e.Graphics.ExcludeClip(r);
}

// continue drawing...
e.Graphics.Clear(Color.Yellow);

或者如果尝试修改控件的区域,则只需使用Region的Exclude属性:

GraphicsPath path = new GraphicsPath();
path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);

Region r = new Region(new Rectangle(Point.Empty, this.ClientSize));
r.Exclude(path);

this.Region = r;

答案 2 :(得分:0)

我不知道如何反转选择,但你总是可以在背景颜色中画一颗心,然后将背景颜色改为黑色。

this.BackColor = Form.BackColor;
Form.BackColor = Color.Black;

答案 3 :(得分:0)

原始解决方案here。我已根据您的需要修改了

 this.Region = InvertRegion(new Region(path), this.Width, this.Height);

private Region InvertRegion(Region region, int width, int height)
{
    Bitmap mask = new Bitmap(width, height);
    Graphics.FromImage(mask).FillRegion(Brushes.Black, region);

    int matchColor = Color.Black.ToArgb();

    Region inverted = new System.Drawing.Region();
    inverted.MakeEmpty();
    Rectangle rc = new Rectangle(0, 0, 0, 0);
    bool inimage = false;
    for (int y = 0; y < mask.Height; y++)
    {
        for (int x = 0; x < mask.Width; x++)
        {
            if (!inimage)
            {
                if (mask.GetPixel(x, y).ToArgb() != matchColor)
                {
                    inimage = true;
                    rc.X = x;
                    rc.Y = y;
                    rc.Height = 1;
                }
            }
            else
            {
                if (mask.GetPixel(x, y).ToArgb()  == matchColor)
                {
                    inimage = false;
                    rc.Width = x - rc.X;
                    inverted.Union(rc);
                }
            }

        }
        if (inimage)
        {
            inimage = false;
            rc.Width = mask.Width - rc.X;
            inverted.Union(rc);
        }
    }
    return inverted;
}