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

A graphics path is a combination of multiple graphics shapes. For example the graphics path in Figure 3.28 is a combination of lines, an ellipse, and a rectangle.

The GraphicsPath class represents graphics paths. It provides methods to add graphics objects. For example, the AddLine, AddRectangle, AddEllipse, AddArc, AddPolygon, AddCurve and AddBezier methods and a line, a rectangle, an ellipse, an arc, a polygon, a curve, and a Bezier curve, respectively.

GraphicsPath is defined in the System.Drawing.Drawing2D namespace. You must import this namespace using the following line:


using
System.Drawing.Drawing2D;

The Graphics class provides a DrawPath method, which draws a graphics path. It takes two arguments: Pen and GraphicsPath.

To draw a graphics path, first we create a GraphicsPath object, then we add graphics shapes to the path by calling its Add methods, and finally we call DrawPath. For example, the following code creates a graphics path adds an ellipse to the path, and draws it.


GraphicsPath graphPath = new GraphicsPath();
graphPath.AddEllipse (50, 50, 100,150);
g.DrawPath (greenPen, graphPath);


Let's add more shapes to the graph. Listing 3.21 creates a graphics path; add some lines, an ellipse, and a rectangle; and draws the path.

LISTING 3.21: Drawing a graphics path


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;
using
System.Drawing.Drawing2D;

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

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            // Create a pen
            Pen greenPen = new Pen(Color.Green, 1);

            // Create a graphics path
            GraphicsPath path = new GraphicsPath();

            // Add a line to the path
            path.AddLine(20, 20, 103, 80);

            // Add an ellipse to the path
            path.AddEllipse(100, 50, 100, 100);

            // Add three more lines
            path.AddLine(195, 80, 300, 80);
            path.AddLine(200, 100, 300, 100);
            path.AddLine(195, 120, 300, 120);

            // Create a rectangle and call AddRectangle
            Rectangle rect = new Rectangle(50, 150, 300, 50);
            path.AddRectangle(rect);

            // Draw path
            e.Graphics.DrawPath(greenPen, path);

            // Dispose of object
            greenPen.Dispose();
        }
    }
}


Figure 3.29 shows the output from Listing 3.21

3.28.gif

FIGURE 3.28: A path

Conclusion


Hope the article would have helped you in understanding how to Draw Graphics Paths 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.