Drawing Ellipses and Circles in GDI+

In this article I will explain you how to draw Ellipses and Circles in GDI+.

This article has been excerpted from book "Graphics Programming with GDI+ ".


FIGURE 3.5: An ellipse

Drawing Ellipses and Circles

An ellipse is a circular boundary within a rectangle, where each opposite point has the same distance from a fixed point, called the center of the ellipse. An ellipse within a square is called a circle. Figure 3.5 shows an ellipse with its height, width, and center indicated.

To draw an ellipse, you need to specify the outer rectangle. GDI+ takes care of the rest. DrawEllipse draws an ellipse defined by a rectangle specified by a pair of coordinates, a height, and a width (an ellipse with equal height and width is a circle). DrawEllipse has four overloaded methods.

void DrawEllipse(Pen, Rectangle);
void DrawEllipse(Pen, RectangleF);
void DrawEllipse(Pen, int, int, int, int);
void DrawEllipse(Pen, float, float, float, float);

To draw an ellipse, an application creates a pen and four coordinates (or a rectangle), and then calls DrawEllipse. Listing 3.5 draws ellipses with different options.

LISTING 3.5: Drawing ellipses

void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
// Create Pens
    Pen redPen = new Pen(Color.Red, 6);
Pen bluePen = new Pen(Color.Blue, 4);
Pen greenPen = new Pen(Color.Green, 2);
// Create a rectangle
    Rectangle rect = new Rectangle(80, 80, 50, 50);
// Draw ellipses
    e.Graphics.DrawEllipse(greenPen, 100.0F, 100.0F, 10.0F, 10.0F);
    e.Graphics.DrawEllipse(redPen, rect);
    e.Graphics.DrawEllipse(bluePen, 60, 60, 90, 90);
    e.Graphics.DrawEllipse(greenPen, 40.0F, 40.0F, 130.0F, 130.0F);
//Dispose of objects

Figure 3.6 shows the output from Listing 3.5


FIGURE 3.6: Drawing ellipse


Hope the article would have helped you in understanding drawing Ellipses and Circles in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.