Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing circles with System.Drawing

Tags:

I have this code that draws a Rectangle ( Im trying to remake the MS Paint )

 case "Rectangle":                if (tempDraw != null)                 {                     tempDraw = (Bitmap)snapshot.Clone();                     Graphics g = Graphics.FromImage(tempDraw);                     Pen myPen = new Pen(foreColor, lineWidth);                     g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1);                     myPen.Dispose();                     e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);                     g.Dispose();                 } 

But what if I want to draw a circle, what will change?

g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1); 
like image 872
Tony Avatar asked Dec 02 '09 18:12

Tony


People also ask

How do you draw a circle in programming?

h contains circle() function which draws a circle with center at (x, y) and given radius. Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle.

Which instrument is used for drawing circles?

The terms compass and divider are often interchanged, for each instrument can be used to draw circles, mark divisions (divide a given distance), or simply mark a distance. Technically, a compass is a drafting instrument that has one pen or pencil point and one sharp point that…


2 Answers

There is no DrawCircle method; use DrawEllipse instead. I have a static class with handy graphics extension methods. The following ones draw and fill circles. They are wrappers around DrawEllipse and FillEllipse:

public static class GraphicsExtensions {     public static void DrawCircle(this Graphics g, Pen pen,                                   float centerX, float centerY, float radius)     {         g.DrawEllipse(pen, centerX - radius, centerY - radius,                       radius + radius, radius + radius);     }      public static void FillCircle(this Graphics g, Brush brush,                                   float centerX, float centerY, float radius)     {         g.FillEllipse(brush, centerX - radius, centerY - radius,                       radius + radius, radius + radius);     } } 

You can call them like this:

g.FillCircle(myBrush, centerX, centerY, radius); g.DrawCircle(myPen, centerX, centerY, radius); 
like image 95
Olivier Jacot-Descombes Avatar answered Sep 28 '22 11:09

Olivier Jacot-Descombes


Try the DrawEllipse method instead.

like image 29
Stephen Wrighton Avatar answered Sep 28 '22 10:09

Stephen Wrighton