Drawing Pie Shapes in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

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


using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;

namespace
DrawingPieShapes
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            // get the current value of start and sweep angles
            float startAngle = (float)Convert.ToDouble(textBox1.Text);
            float sweepAngle = (float)Convert.ToDouble(textBox2.Text);

            // Create a pen
            Pen bluePen = new Pen(Color.Blue, 1);

            // Draw pie
            g.DrawPie(bluePen, 20, 20, 100, 100, startAngle, sweepAngle);

            // dispose of objects
            bluePen.Dispose();
            g.Dispose();
        }
    }
}


3.30.gif

FIGURE 3.30: Four pie shapes of an ellipse

3.31.gif

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.

3.32.gif

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

3.33.gif

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

3.34.gif

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

Conclusion

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.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all