Drawing Pie Shapes in GDI+ using VB.NET

In this article I will explain you how to Draw Pie Shapes in GDI+.
  • 2991

A pie is a slice of an ellipse. A pie shape also consists of two radial lines that intersect with the endpoints of the arc. Figure 3.30 shows an ellipse with four pie shapes.

The Graphics class provides the DrawPie method, which draws a pie shape defined by an arc of an ellipse. The DrawPie method takes a Pen object, a Rectangle or RectangleF object, and two radial angles.

Let's create an application that draws pie shapes. We create a Windows application and add two text boxes and a button control to the form. The final form looks like Figure 3.31.

The DrawPie button will draw a pie shape based on the values entered in the Start Angle and Sweep Angle text boxes. Listing 3.22 shows the code for the DrawPie button click event handler.

LISTING 3.22: Drawing a pie shape

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Create a Graphics object
        Dim g As Graphics = Me.CreateGraphics()

        ' get the current value of start and sweep angles
        Dim startAngle As Single = CSng(Convert.ToDouble(textBox1.Text))
        Dim sweepAngle As Single = CSng(Convert.ToDouble(textBox2.Text))

        ' Create a pen
        Dim bluePen As New Pen(Color.Blue, 1)

        ' Draw pie
        g.DrawPie(bluePen, 20, 20, 100, 100, startAngle, _

        ' dispose of objects
    End Sub
End Class


FIGURE 3.30: Four pie shapes of an ellipse


FIGURE 3.31: A pie shape-drawing application

Now let's run the pie shape-drawing application and enter values for the start and sweep angles. Figures 3.32 shows a pie for start and sweep angles of 0.0 and 90 degrees respectively.

Figures 3.33 shows a pie for start and sweep angles of 45.0 and 180.0 degrees respectively.

Figure 3.34 shows a pie for start and sweep angles of 90.0 and 45.0 degrees, respectively.


FIGURE 3.32: A pie shape with start angle of 0 degrees and sweep angle of 90 degrees


FIGURE 3.33: A pie shape with start angle of 45 degrees and sweep angle of 180 degrees

FIGURE 3.34: A pie shape with start angle of 90 degrees and sweep angle of 45 degrees


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


More Articles

© 2020 DotNetHeaven. All rights reserved.