VB.NET 1.1 Tutorial - GDI+ - Part 2 - Lines


In this part we will look at some simple applications that utilise the method DrawLine.

Lines with Coordinates

The first version of the DrawLine method draws a line connecting two points specified by coordinate pairs. The syntax for the method definition is:

Overloads Public Sub DrawLine( _
  ByVal pen As Pen, _
  ByVal x1 As Integer, _
  ByVal y1 As Integer, _
  ByVal x2 As Integer, _
  ByVal y2 As Integer _
)

Where (x1, y1) is the starting point of the line and (x2, y2) is the end point of the line. The coordinates do not have to be integers, as there is an alternative method which accepts Single values instead.

Overloads Public Sub DrawLine( _
  ByVal pen As Pen, _
  ByVal x1 As Single, _
  ByVal y1 As Single, _
  ByVal x2 As Single, _
  ByVal y2 As Single _
) 

We will modify the example from the previous page to draw a line underneath the text. As seen above, we will need to use a pen object to draw our line and our coordinates will be integers.

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

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

  ' Create Font
  Dim myFont As New Font( "Verdana", 20 )

  ' Create Brush
  Dim myBrush As New SolidBrush( Color.Tomato )

  ' Draw the String
  g.DrawString("Hello Mum!", myFont, myBrush, 40, 40)

  ' Create Pen
  Dim myPen As New Pen( Color.Black, 3 )

  ' Draw the Line
  g.DrawLine( myPen, 40, 75, 200, 75 )

  ' Now tidy up
  myFont.Dispose()
  myBrush.Dispose()
  myPen.Dispose()
End Sub

This code gives the following output:

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

Lines with Points

Before we look at the next two versions of the DrawLine method, take a quick look at the Point class. Both of the following methods take two pairs of coordinates defined with either the Point of PointF class.

public void DrawLine( Pen, Point pt1, Point pt2 );

or

public void DrawLine( Pen, PointF pt1, PointF pt2 );

We could replace the line in the application code with the following, and we would not notice any difference in our application when it is run.

g.DrawLine( p, new Point(40, 75), new Point(200, 75) );

Lines with Style

Before we leave lines, I thought we would touch on the DashStyle Enumeration. If we set this property of the pen class, we can draw different types of lines.

This enumeration has five pre-defined line types, plus the ability to define a custom line type. The five pre-defined types are:

  • Solid
  • Dash
  • Dot
  • DashDot
  • DashDot

They are shown in the screen shot below.

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


References

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

For more information on the DashStyle Enumeration, visit MSDN at microsoft here.

What Next?

Next we will draw some simple arcs.

Return to the Tutorial Contents.