Any way to easily draw an empty donut shape in C #

So what about the graphics. FillEllipse, but with a hole in the middle. I need to highlight some circular icons by placing a ring around them, and due to the limitations of a larger program, it is difficult or impossible to just back up FillEllipse so that it looks like a hole.

+4
source share
5 answers

Using GDI +, you can draw a circle with a high pen width so that it looks like a donut. There will be nothing in the center, so you can see it.

+14
source
// Create a brush SolidBrush b = new SolidBrush(Color.Blue); // Clear your Graphics object (defined externally) gfx.Clear(Color.White); // You need a path for the outer and inner circles GraphicsPath path1 = new GraphicsPath(); GraphicsPath path2 = new GraphicsPath(); // Define the paths (where X, Y, and D are chosen externally) path1.AddEllipse((float)(X - D / 2), (float)(Y - D / 2), (float)D, (float)D); path2.AddEllipse((float)(X - D / 4), (float)(Y - D / 4), (float)(D / 2), (float)(D / 2)); // Create a region from the Outer circle. Region region = new Region(path1); // Exclude the Inner circle from the region region.Exclude(path2); // Draw the region to your Graphics object gfx.FillRegion(b, region); 
+13
source

You can create a Region based on what you would draw using FillEllipse, and use the Exclude method in the region to remove areas you don't need using another GraphicsPath returned from another FillEllipse call.

Then you just need to overlay the resulting region on top of what you want it to surround.

+3
source

Based on user response263134:

  g.FillRegion(Brushes.Black, GetRingRegion(center, innerRadius, outherRadius)); public static RectangleF GetRectangle(PointF center, float radius) { var rectangle = new RectangleF(center.X - radius, center.Y - radius,radius * 2, radius * 2); return rectangle; } public static Region GetRingRegion(PointF center, float innerRadius, float outherRadius) { // You need a path for the outer and inner circles var path1 = new GraphicsPath(); var path2 = new GraphicsPath(); // Define the paths (where X, Y, and D are chosen externally) path1.AddEllipse(GetRectangle(center,outherRadius)); path2.AddEllipse(GetRectangle(center, innerRadius)); // Create a region from the Outer circle. Region region = new Region(path1); // Exclude the Inner circle from the region region.Exclude(path2); return region; } 
0
source

The answer "sth" is pretty much what you need, but a dude you can easily use: (Graphics) .DrawEllipse (new pen (YOURCOLOR, RINGDIAMETER), center.x - radius, center.y - radius, radius * 2, radius * 2); But I think you knew that before. :)

0
source

All Articles