2

我有一个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

我希望它看起来像什么(白色=透明): 在此处输入图像描述

4

4 回答 4

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);
}

结果是:

在背景图像上绘制黑心

于 2012-10-23T22:12:49.110 回答
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;
于 2012-10-23T22:03:12.797 回答
0

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

this.BackColor = Form.BackColor;
Form.BackColor = Color.Black;
于 2012-10-23T21:53:26.800 回答
0

原始解决方案在这里。我已根据您的需要对其进行了修改

 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;
}
于 2012-10-23T22:06:50.173 回答