VB.NET 1.1 Tutorial - GDI+ - Part 3 - Arcs


In this part we will look at some simple applications that utilise the method DrawArc.

Arcs with Rectangles

The first two versions of the DrawArc method we shall look at, takes four parameters. The syntax for the method definition is:

Public Sub DrawArc( _
  ByVal pen As Pen, _
  ByVal rect As Rectangle, _
  ByVal startAngle As Single, _
  ByVal sweepAngle As Single _
)

or

Public Sub DrawArc( _
  ByVal pen As Pen, _
  ByVal rect As RectangleF, _
  ByVal startAngle As Single, _
  ByVal sweepAngle As Single _
)

Where the Parameters are:

  • pen - Pen object that determines the color, width, and style of the arc.
  • rect - Rectangle structure that defines the boundaries of the ellipse.
  • startAngle - Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
  • sweepAngle - Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.

These methods draw an arc that is a portion of the perimeter of an ellipse. The ellipse is defined by the boundaries of a rectangle. The arc is the portion of the perimeter of the ellipse between the startAngle parameter and the startAngle + sweepAngle parameters.

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

Private Sub ArcDemo_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 to bound arc.
  Dim rect As New Rectangle( 30, 30, 200, 100)

  ' Create start and sweep angles on arc.
  Dim startAngle As Integer = 45
  Dim sweepAngle As Integer = 180

  ' Draw arc to screen.
  g.DrawArc( myPen, rect, startAngle, sweepAngle)

  ' Now tidy up
  myPen.Dispose()
End Sub

This code gives the following output:

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

Arcs with values

The other two methods just use integers or floating point values to define the parameters.

Public Sub DrawArc( _
  ByVal pen As Pen, _
  ByVal x As Integer, _
  ByVal y As Integer, _
  ByVal width As Integer, _
  ByVal height As Integer, _
  ByVal startAngle As Integer, _
  ByVal sweepAngle As Integer _
)

or

Public Sub DrawArc( _
  ByVal pen As Pen, _
  ByVal x As Single, _
  ByVal y As Single, _
  ByVal width As Single, _
  ByVal height As Single, _
  ByVal startAngle As Single, _
  ByVal sweepAngle 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 that defines the ellipse.
  • y - y-coordinate of the upper-left corner of the rectangle that defines the ellipse.
  • width - Width of the rectangle that defines the ellipse.
  • height - Height of the rectangle that defines the ellipse.
  • startAngle - Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
  • sweepAngle - Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.

Arcs with Style

Before we leave arcs, I thought we would show that the DashStyle Enumeration can be applied to arcs as well as lines.

The screen shot below shows arcs with different styles.

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


References

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

For more information on the DashStyle Enumeration, visit MSDN at microsoft here.

What Next?

Next we will draw some simple Rectangles.

Return to the Tutorial Contents.