Drawing Lines in GDI+ using VB.NET

In this article I will explain you how to draw lines in GDI+.
  • 7365
The DrawLine method draws a line between two points specified by a pair coordinates. DrawLines draws a series of lines using an array of points.

To draw a line, an application first creates a Pen object, which defines the color and width. The following line of code creates a red pen with a width of 1:

Dim redPen As New Pen(Color.Red, 1)

After that we define the endpoints of the line:

        Dim x1 As Single = 20.0F, y1 As Single = 25.0F
        Dim x2 As Single = 200.0F, y2 As Single = 100.0F

Finally, we use the pen and points as input to DrawLine:

.DrawLine(redPen, x1, y1, x2, y2)

Listing 3.1 shows how to use the different overloaded methods. We create four pens with different colors and widths. After that we call DrawLine with different values-including integer, floating point, and Point structures-to draw four different lines. Three of them start at point (20,20).

LISTING 3.1: Drawing lines

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        ' Create four Pen objects with red,
        ' blue, green, and black colors and
        ' different widths
        Dim redPen As New Pen(Color.Red, 1)
        Dim bluePen As New Pen(Color.Blue, 2)
        Dim greenPen As New Pen(Color.Green, 3)
        Dim blackPen As New Pen(Color.Black, 4)

        ' Draw line using float coordinates
        Dim x1__1 As Single = 20.0F, y1__2 As Single = 20.0F
        Dim x2__3 As Single = 200.0F, y2__4 As Single = 20.0F
        e.Graphics.DrawLine(redPen, x1__1, y1__2, x2__3, y2__4)

        ' Draw line using Point structure
        Dim p1 As New Point(20, 20)
        Dim p2 As New Point(20, 200)
        e.Graphics.DrawLine(greenPen, p1, p2)

        'Draw line using PointF structures
        Dim ptf1 As New PointF(20.0F, 20.0F)
        Dim ptf2 As New PointF(200.0F, 200.0F)
        e.Graphics.DrawLine(bluePen, ptf1, ptf2)

        ' Draw line using integer coordinates
        Dim X1__5 As Integer = 60, Y1__6 As Integer = 40, X2__7 As Integer = 250, Y2__8 As Integer = 100
        e.Graphics.DrawLine(blackPen, X1__5, Y1__6, X2__7, Y2__8)

        'Dispose of objects
    End Sub

End Class

The output from listing 3.1 is shown in Figure 3.1. We've drawn four lines starting at point (20,20).

Drawing Connected Lines

Sometimes we need to draw multiple connected straight line segments. One way to do this is to call the DrawLine method multiples times.


FIGURE 3.1: Using DrawLine to draw lines

To draw lines using DrawLines, an application first creates a Pen object, then creates an array of points, and then calls DrawLines. The code in Listing 3.2 draws three line segments.

LISTING 3.2: Using DrawLines to draw connected lines

        Dim ptsArray As PointF() = {New PointF(20.0F, 20.0F), New PointF(20.0F, 20.0F), New PointF(200.0F, 200.0F), New PointF(20.0F, 20.0F)}

        e.Graphics.DrawLines(redPen, ptsArray)

The code in Listing 3.2 draws what is shown in Figure 3.2.


FIGURE 3.2: Using DrawLines to draw connected lines


Hope the article would have helped you in understanding drawing lines. Read other articles on GDI+ on the website.


More Articles

© 2020 DotNetHeaven. All rights reserved.