简体   繁体   中英

How do I fill a rectangle with the exception of a area

I am trying to fill a rectangle in a winforms application less a ellipse in the center that allows the image in the background to show through.

can anyone give me a hint on which way to go on this,

thanks.

this is what I have come up with so far:

  path.AddRectangle(new Rectangle(30, 30, 100, 100));
  path.AddEllipse(new Rectangle(50, 50, 60, 60));
  gfx.FillPath(new SolidBrush(Color.Black), path);

替代文字

protected override void OnPaint(PaintEventArgs e){
    var rgn  = new Region(new Rectangle(50, 50, 200, 100));
    var path = new GraphicsPath();
    path.AddEllipse(60, 60, 180, 80);
    rgn.Exclude(path);
    e.Graphics.FillRegion(Brushes.Blue, rgn);
}

The easy way:

  • Fill the Reacngle first
  • Then Fill the Ellipse (with a Transparant brush)

It isn't clear enough what kind of transparency is required there. The simple way is to invert the problem. Use a TextureBrush to draw the image with Graphics.FillEllipse().

You could try to use regions . Create a rectangle region, exclude an ellipse and then fill it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM