Visual Basic 2005 Guide
Using Graphics Paths

Drawing Polygons

The following code can be used to draw a polygon in a picture box named picPoly.

Dim g As Graphics = picPoly.CreateGraphics()
Dim ptsArray As Point() = {New Point(0, 0), New Point(30, 0), New Point(30, 20), New Point(45, 50), New Point(20, 50)}
g.DrawPolygon(Pens.Blue, ptsArray)

Study the code carefully before you use it. Look at how the array of Point structures is created - this is an alternative way of declaring and intitialising arrays. The first two lines of code create a reference to the graphics object (always needed for drawing) and clear the contents of the picturebox to allow repainting to be done.

Using The GraphicsPath Class

A GraphicsPath can be used to create and store complex shapes. Up to this point, you will have been making your images by drawing items one after the other. Sometimes, you need a bit more flexibility and, by using the GraphicsPath class, you can have it.

Set up a new project and add a picturebox and button to the form. Call the picturebox, picFace.

At the very top of the code window (before everything else), type the following line of code,

Imports System.Drawing.Drawing2D

This statement will save us a little effort when working with GraphicsPaths. Without this line of code, we will have to write System.Drawing.Drawing2D.GraphicsPath each time we want to use one.

Now to create a function to draw ourselves a shape.

Private Function makeFace() As GraphicsPath
   Dim gP As GraphicsPath
   gP = New GraphicsPath()
   gP.AddEllipse(New Rectangle(0, 0, 100, 100))
   gP.AddEllipse(New Rectangle(20, 20, 20, 20))
   gP.AddEllipse(New Rectangle(60, 20, 20, 20))
   gP.AddEllipse(New Rectangle(45, 40, 10, 10))
   Return gP
End Function

Study the code carefully once you have typed it up. Notice that this function creates and returns a GraphicsPath. In this case we are only adding ellipses (circles) but you can add many more shapes.

In order to draw the path out, we will need to write some code in the event handler for the button click.

Dim gpFace As GraphicsPath
gpFace = makeFace()
Dim g As Graphics = picFace.CreateGraphics()
g.FillPath(Brushes.Red, gpFace)
g.DrawPath(Pens.Black, gpFace)

We fill the path before we draw it. This allows us to have an outlined shape.

Making A More Flexible Function

The following function allows you to create a GraphicsPath to be placed at the coordinates of your choice.

Private Function positionFace(ByVal intX As Integer, ByVal intY As Integer) As GraphicsPath
   Dim gP As GraphicsPath
   gP = New GraphicsPath()
   gP.AddEllipse(New Rectangle(intX, intY, 100, 100))
   gP.AddEllipse(New Rectangle(intX + 20, intY + 20, 20, 20))
   gP.AddEllipse(New Rectangle(intX + 60, intY + 20, 20, 20))
   gP.AddEllipse(New Rectangle(intX + 45, intY + 40, 10, 10))
   Return gP
End Function

Add another button and add the following code to its click event handler,

Dim gpfaceArray(3) As GraphicsPath
Dim g As Graphics = picFace.CreateGraphics()
Dim intCounter As Integer
For intCounter = 0 To 3
   gpfaceArray(intCounter) = positionFace(intCounter * 100, 0)
   g.DrawPath(Pens.Black, gpfaceArray(intCounter))

This is obviously just one way to go about doing this. Have a bit of a play with the types of shape that you can make and the way that you position them.