SIGN UP MEMBER LOGIN:    
ARTICLE

Drawing Graphics Paths in GDI+

Posted by Mahesh Chand Articles | GDI+ & Graphics November 27, 2009
In this Article I will explain how to Draw Graphics Paths in GDI+.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor