VB.NET 1.1 Tutorial - GDI+ - Part 6 - Paths


In this part we will look at some simple applications that utilise the methods DrawPath and FillPath.

Drawing Paths

Drawing lines and rectangles works very well when you need to create custom Windows Forms controls. If you want to create more complex and artistic drawings such as diagrams, GDI+ lets you draw more complex shapes. In GDI+, this is accomplished using graphics paths. The DrawPath method is used to draw a GraphicsPath object.

A GraphicsPath object encapsulates a number of line segments. You add individual segments via drawing primitives, such as AddElipse() and AddLine(). GraphicsPath objects make it relatively simple to generate complex shapes by automatically connecting line segments.

The DrawPath method does not have more than one version unlike the previous drawing methods.

Public Sub DrawPath( _
  ByVal pen As Pen, _
  ByVal path As GraphicsPath _
)

Where the Parameters are:

  • pen - Pen object that determines the color, width, and style of the path.
  • GraphicsPath - GraphicsPath object to draw.

One point to note is that the GraphicsPath object is part of the System.Drawing.Drawing2D namespace, so we need to include the following in our application.

Imports System.Drawing.Drawing2D

As before, we will create a simple example to draw a GraphicsPath on the form.

Private Sub PathDemo_Paint( ByVal sender As Object, _
                            ByVal e As PaintEventArgs) _
                            Handles MyBase.Paint

  ' Get Graphics Object
  Dim g As Graphics = e.Graphics

  ' Create Pen
  Dim myPen As New Pen( Color.Blue, 2 )

  ' Create graphics path object and add ellipse
  Dim graphPath As New GraphicsPath()
  graphPath.AddEllipse(46, 4, 28, 28)
  graphPath.AddLine(36, 32, 84, 32)
  graphPath.AddLine(100, 80, 88, 84)
  graphPath.AddLine(76, 50, 74, 84)
  graphPath.AddLine(90, 150, 74, 150)
  graphPath.AddLine(60, 100, 46, 150)
  graphPath.AddLine(32, 150, 46, 84)
  graphPath.AddLine(44, 50, 32, 84)
  graphPath.AddLine(20, 80, 36, 32)

  ' Draw path to screen
  g.DrawPath( myPen, graphPath )

  ' Now tidy up
  myPen.Dispose()
End Sub

This code gives the following output:

You can download the file path01.vb from the link, or you can download my copy of the executable from here.

Filling Paths

The FillPath method also only has one version.

Public Sub FillPath( _
  ByVal brush As Brush, _
  ByVal path As GraphicsPath _
)

Where the Parameters are:

  • brush - Brush object that is used to paint the interior of the path.
  • path - GraphicsPath object to draw.

So we modify the previous example to use a LinearGradientBrush to fill the interior of the person.

Private Sub PathDemo_Paint( ByVal sender As Object, _
                            ByVal e As PaintEventArgs) _
                            Handles MyBase.Paint

  ' Get Graphics Object
  Dim g As Graphics = e.Graphics

  ' Create graphics path object and add ellipse.
  Dim graphPath As New GraphicsPath()
  graphPath.AddEllipse(46, 4, 28, 28)
  graphPath.AddLine(36, 32, 84, 32)
  graphPath.AddLine(100, 80, 88, 84)
  graphPath.AddLine(76, 50, 74, 84)
  graphPath.AddLine(90, 150, 74, 150)
  graphPath.AddLine(60, 100, 46, 150)
  graphPath.AddLine(32, 150, 46, 84)
  graphPath.AddLine(44, 50, 32, 84)
  graphPath.AddLine(20, 80, 36, 32)

  ' Create Rectangle To Limit brush area.
  Dim rect As New Rectangle(20, 0, 100, 150)

  ' Create Brush
  Dim linearBrush As New LinearGradientBrush( 
                                            rect, _
                                            Color.Red, _
                                            Color.Yellow, _
                                            LinearGradientMode.BackwardDiagonal)

  ' Draw path to screen.
  g.FillPath( linearBrush, graphPath )

  ' Now tidy up
  linearBrush.Dispose()
End Sub

This code gives the following output:

You can download the file path02.vb from the link, or you can download my copy of the executable from here.


References

For more information on the DrawPath Method, visit MSDN at microsoft here.

For more information on the FillPath Method, visit MSDN at microsoft here.

What Next?

Next we will look at aanother simple event driven application.

Return to the Tutorial Contents.