1. Home
  2. Computing & Technology
  3. Visual Basic

GDI+ Graphics in Visual Basic 2005 .NET
Using the GraphicsPath Object

By , About.com Guide

Mar 4 2007

The examples so far have used a lot of drawing statements like:

Dim R As New Rectangle(50, 50, 100, 100)
G.DrawRectangle(Pens.Black, R)

So you can draw circles and square of any size anywhere on your Graphics object. But where's the fun in that? Real graphics are more than just circles and squares!

One answer is GraphicsPath. This object lets you combine shapes and treat them as a single shape. You can then fill the shape with images or color, use it as a clipping region (explained next), or move it around as we saw in Part 5. Let's draw a random path using a GraphicsPath object. The first thing we need to do is create a set of Point objects. I invented this set more or less at random.

Dim myPathPoints As Point() = _
   {New Point(50, 50), _
    New Point(220, 110), _
    New Point(125, 165), _
    New Point(115, 345), _
    New Point(45, 115)}

The easiest way to simply draw a line connecting all these points is to use the DrawLines method of the Graphics object. If G is a Graphics object and PathPen is a Pen object, this will draw a connected line from the first to the last point.

G.DrawLines(PathPen, myPathPoints)

But this doesn't give you a GraphicsPath! Each point in a GraphicsPath has a type, called the PathPointType, associated with it to enable the magic that can be done with a GraphicsPath. If you think about it, a lot of different lines could be drawn through a set of points so there has to be some additional information for VB.NET to use. The DrawLines method simply assumes that they're all straight line segments. To do the same thing with a GraphicsPath object, I have assigned the Line type to all of the points. Here's the code:

Private Sub DrawPath_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles DrawPath.Click
   Using G As Graphics = Me.CreateGraphics
   Dim myPathPoints As Point() = _
      {New Point(50, 50), _
       New Point(220, 110), _
       New Point(125, 165), _
       New Point(115, 345), _
       New Point(45, 115)}
   Dim myPointTypes(4) As Byte
   'Initialize all of the PathPointType to Line
   For i As Integer = 0 To 4
      myPointTypes(i) = CByte(PathPointType.Line)
   Next i
   Dim myPath As GraphicsPath = _
      New GraphicsPath(myPathPoints, myPointTypes)
   Dim PathPen As Pen = New Pen(Color.Tomato, 3)
   ' This draws the same line
   'G.DrawLines(PathPen, myPathPoints)
   G.DrawPath(PathPen, myPath)
   End Using
End Sub

On the next page, a series of shapes are added to a GraphicsPath and then two GraphicsPath objects are compared in a Boolean operation to create an XOR'ed clipping region.

Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. GDI+ Graphics in Visual Basic 2005 .NET

©2010 About.com, a part of The New York Times Company.

All rights reserved.