Search code examples
c#winformsdrawing

System Drawing Invert Region build out of Path


Ive got a panel and im drawing a heart on that panel.. But i dont want to draw the heart i want to draw everything except the heart so the heart is transparent. Can i invert the Region selected out of the 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;

What it looks like(white = transparent):1

What i want it to look like(white = transparent): enter image description here


Solution

  • I think you can just add 2 graphics paths together.

    You could try this code out:

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

    Result is:

    Drawing the black heart on top of a background image