Tutorial :Drawing circles with System.Drawing



Question:

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


Solution:1

Try the DrawEllipse method instead.


Solution:2

There is no DrawCircle method; use DrawEllipse instead. I have a static class with graphics extension methods (among others not shown here) that 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 it like this:

g.DrawCircle(myPen, centerX, centerY, radius);  


Solution:3

You'll need to use DrawEllipse if you want to draw a circle using GDI+.

An example is here: http://www.websupergoo.com/helpig6net/source/3-examples/9-drawgdi.htm


Solution:4

You should use DrawEllipse:

//  // Summary:  //     Draws an ellipse defined by a bounding rectangle specified by coordinates  //     for the upper-left corner of the rectangle, a height, and a width.  //  // Parameters:  //   pen:  //     System.Drawing.Pen that determines the color, width,  //      and style of the ellipse.  //  //   x:  //     The x-coordinate of the upper-left corner of the bounding rectangle that  //     defines the ellipse.  //  //   y:  //     The y-coordinate of the upper-left corner of the bounding rectangle that  //     defines the ellipse.  //  //   width:  //     Width of the bounding rectangle that defines the ellipse.  //  //   height:  //     Height of the bounding rectangle that defines the ellipse.  //  // Exceptions:  //   System.ArgumentNullException:  //     pen is null.  public void DrawEllipse(Pen pen, int x, int y, int width, int height);  


Solution:5

if you want to draw circle on button then this code might be use full. else if you want to draw a circle on other control just change the name of control and also event. like here event button is called. if you want to draw this circle in group box call the Groupbox event. regards

    public Form1()      {          InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)      {          this.button1.Location = new Point(108, 12);         // this.Paint += new PaintEventHandler(Form1_Paint);          this.button1.Paint += new PaintEventHandler(button1_Paint);      }      void button1_Paint(object sender, PaintEventArgs e)      {          Graphics g = this.button1.CreateGraphics();          Pen pen = new Pen(Color.Red);          g.DrawEllipse(pen, 10, 10, 20, 20);        }            }  


Solution:6

With this code you can easily draw a circle... C# is great and easy my friend

public partial class Form1 : Form  {      public Form1()      {          InitializeComponent();      }      private void button1_Click(object sender, EventArgs e)      {          Graphics myGraphics = base.CreateGraphics();          Pen myPen = new Pen(Color.Red);          SolidBrush mySolidBrush = new SolidBrush(Color.Red);          myGraphics.DrawEllipse(myPen, 50, 50, 150, 150);      }   }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »