Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to easily draw a hollow donut shape in C#

Tags:

c#

So like a graphics.FillEllipse, but with a hole in the middle. I need to highlight some circular icons by putting a ring around them, and due to the constraints of the larger program it's hard/impossible to simply FillEllipse under them to make it look like there's a hole.

like image 810
ck_ Avatar asked Nov 27 '22 02:11

ck_


2 Answers

// 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);
like image 169
user263134 Avatar answered Dec 19 '22 06:12

user263134


Using GDI+, you can draw a circle with a high value for the pen width, to make it look like a donut. There will be nothing in the centre so you'll be able to see through it.

like image 24
Neil Barnwell Avatar answered Dec 19 '22 05:12

Neil Barnwell