ARTICLE

Drawing Lines in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET November 16, 2009
In this article I will explain you how to draw lines in GDI+.
Download Files:
 
Reader Level:

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:


Graphics
.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


Imports System.Drawing.Graphics
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
        redPen.Dispose()
        bluePen.Dispose()
        greenPen.Dispose()
        blackPen.Dispose()
    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.

DrawingLines.JPG

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.

DrawingLines1.JPG

FIGURE 3.2: Using DrawLines to draw connected lines

Conclusion

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

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor