Drawing Arcs in GDI+ using VB.NET

In this article I will explain how to draw arcs in GDI+.
  • 5253

An arc is a portion of an ellipse. For example, Figure 3.12 shows an ellipse that has six arcs. An arc is defined by bounding rectangle (just as an ellipse), a start angle, and a sweep angle. The start angle is an angle in degree measured clockwise from the x-axis to the starting point of the arc.

Figure203_12.jpg

FIGURE 3.12: Arcs in an ellipse

The sweep angle is an angle in degree measured clockwise from the startAngle parameter to the ending point of the arc. So an arc is the portion of the perimeter of the ellipse between the start angle and the start angle plus the sweep angle.

The DrawArc method draws an arc on a graphics surface. DrawArc takes a pen, a pair of coordinates, a width, and a height.


The Pen object determines the color, width, and style of the arc; Rectangle or RectangleF represents the bounding rectangle; and the last two parameters are the start angle and sweep angle.

To draw an arc, the application creates Pen and Rectangle objects and defines start and sweep angles. Then it calls the DrawArc method.

Let's create an application that will draw an arc to match the values of the start and sweep angles. We create a Windows application, adding add two
text boxes and a button control. The final form look like Figure 3.13.

We define two floating variable on the class level to store the start and sweep angles:

fig3_13.gif

FIGURE 3.13: A sample arc application

        Private startAngle As Single = 45.0F
        Private sweepAngle As Single = 90.0F


Note let's draw an arc on the form's pain event handler. Listing 3.11 draws an arc. We first create a pen and a rectangle, and we use them in the DrawArc method with start and sweep angles.

LISTING 3.11: The paint event handler


    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim redPen As New Pen(Color.Red, 3)
        Dim rect As New Rectangle(20, 20, 200, 200)
        e.Graphics.DrawArc(redPen, rect, startAngle, sweepAngle)
        redPen.Dispose()
    End Sub


New we add code for the Reset Angles button. Listing 3.12 simply sets the start and sweep angles by reading values from the text boxes and calls the Invalidate method, which forces GDI+ to call the form's paint event handler:

LISTING 3.12: The Reset Angles button click event handler


    Private Sub ResetAnglesBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        startAngle = CSng(Convert.ToDouble(textBox1.Text))
        sweepAngle = CSng(Convert.ToDouble(textBox2.Text))
        Invalidate()
    End Sub


Figure 3.14 shows the default output from the application

Now let's change the start and sweep angles to 90 and 180 degrees, respectively, and click the Reset Angles button. The new output looks like Figure 3.15.

fig3_14.gif

FIGURE 3.14: The default arc, with start angle 45 degrees and sweep angle with 90 degree

fig3_15.gif

FIGURE 3.15: An arc with start angle 90 degrees and sweep angle of 180 degrees

Let's changes angles one more time. This time our start angle will be 180 degrees, and the sweep angle will be 360 degrees. The new output looks like Figure 3.16.

Conclusion


Hope the article would have helped you in understanding how to to draw arcs in GDI+. Read other articles on GDI+ on the website.
  

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.