VB.NET 1.1 Tutorial - GDI+ - Part 5 - Ellipses


In this part we will look at some simple applications that utilise the methods DrawEllipse and FillEllipse.

Drawing Ellipses

There are two methods of drawing an ellipse. The first two versions of the DrawEllipse method, takes five parameters. The syntax for the method definition is:

Public Sub DrawEllipse( _
  ByVal pen As Pen, _
  ByVal x As Integer, _
  ByVal y As Integer, _
  ByVal width As Integer, _
  ByVal height As Integer _
)

or

Public Sub DrawEllipse( _
  ByVal pen As Pen, _
  ByVal x As Single, _
  ByVal y As Single, _
  ByVal width As Single, _
  ByVal height As Single _
)

Where the Parameters are:

  • pen - Pen object that determines the color, width, and style of the arc.
  • x - x-coordinate of the upper-left corner of the rectangle to draw.
  • y - y-coordinate of the upper-left corner of the rectangle to draw.
  • width - Width of the rectangle to draw.
  • height - Height of the rectangle to draw.

These methods draw an ellipse specified by a coordinate pair, a width, and a height.

We will create a simple example to draw an ellipse on the form.

Private Sub EllipseDemo_Paint( ByVal sender As Object, _
                               ByVal e As PaintEventArgs) _
                               Handles MyBase.Paint

  ' Get Graphics Object
  Dim g As Graphics = e.Graphics

  ' Create Pen
  Dim myPen As New Pen( Color.Blue, 2 )

  ' Draw Ellipse to screen
  g.DrawEllipse( myPen, 10, 10, 220, 150 )

  ' Now tidy up
  myPen.Dispose()
End Sub 

This code gives the following output:

You can download the file ellip01.vb from the link, or you can download my copy of the executable from here.

Ellipses using Rectangle

The other methods use a Rectangle or RectangleF structure to draw an ellipse. So to create the same ellipse as before, the code would like this.

Private Sub EllipseDemo_Paint( ByVal sender As Object, _
                               ByVal e As PaintEventArgs) _
                               Handles MyBase.Paint

  ' Get Graphics Object
  Dim g As Graphics = e.Graphics

  ' Create Pen
  Dim myPen As New Pen( Color.Blue, 2 )

  ' Create rectangle
  Dim rect As New Rectangle( 10, 10, 220, 150 )

  ' Draw Ellipse to screen
  g.DrawEllipse( myPen, rect )

  ' Now tidy up
  myPen.Dispose()
End Sub

Filling Ellipses

The FillEllipse method is similar to the DrawEllipse method. There are essentially two ways of using the FillEllipse method. The first two versions take a brush and four integers or floating-point values. The other two versions take a brush and a Rectangle or RectangleF structure.

Below is the code to draw a filled ellipse of the same size as before.

Private Sub EllipseDemo_Paint( ByVal sender As Object, _
                               ByVal e As PaintEventArgs) _
                               Handles MyBase.Paint

  ' Get Graphics Object
  Dim g As Graphics = e.Graphics

  ' Create Brush
  Dim myBrush As New SolidBrush(Color.Blue)

  ' Draw Ellipse to screen
  g.DrawEllipse( myBrush, 10, 10, 220, 150 )

  ' Now tidy up
  myBrush.Dispose()
End Sub

You can download the file ellip02.vb from the link, or you can download my copy of the executable from here.


References

For more information on the DrawEllipse Method, visit MSDN at microsoft here.

For more information on the FillEllipse Method, visit MSDN at microsoft here.

What Next?

Next we will draw some simple paths.

Return to the Tutorial Contents.