Drawing GDI+ Ellipses and Circles in VB.NET

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

Ellipse1.jpg


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

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


    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        ' Create Pens
        Dim redPen As New Pen(Color.Red, 6)
        Dim bluePen As New Pen(Color.Blue, 4)
        Dim greenPen As New Pen(Color.Green, 2)
        ' Create a rectangle
        Dim rect As 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
        redPen.Dispose()
        greenPen.Dispose()
        bluePen.Dispose()
    End Sub

Figure 3.6 shows the output from Listing 3.5

Ellipse2.jpg

FIGURE 3.6: Drawing ellipse

Conclusion

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.