ARTICLE

Getting and Setting Line Caps and Styles in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET May 28, 2010
In this article I will explain about Getting and Setting Line Caps and Styles in GDI+.
 
Reader Level:

HTML clipboard

We create aWindowsapplication and a MainMenu control with three menu items on the form. We call these menu items GetCapStyle, LineDashStyle, and LineDashCap, respectively, and write menu click event handlers by double-clicking on them. On the GetCapStyle menu item click event handler, we will read different line caps and generate output using these line caps; on the LineDashStyle menu item click event handler, we will generate lines with different dash styles; and on the LineDashCap menu item click event handler, we will generate output with different line dash caps.
 
The GetCapStyle menu item click event handler is shown in Listing 9.2. We create a pen and set the starting and ending caps using the StartCap and EndCap properties of the Pen Object, and then we draw a line.
 
LISTING 9.2: Getting line caps

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Namespace GettingSetting_Line_CapsStyles
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Paint(ByVal sender As Object,ByVal e As PaintEventArgs)
            Dim g As Graphics = Me.CreateGraphics()
            g.Clear(Me.BackColor)
            ' Create a pen
            Dim blackPenAs New Pen(Color.Black, 10)
            ' Set line styles
            blackPen.StartCap = LineCap.Triangle
            blackPen.EndCap = LineCap.Triangle
            g.DrawLine(blackPen, 20, 10, 200, 10)
            blackPen.StartCap = LineCap.Square
            blackPen.EndCap = LineCap.AnchorMask
            g.DrawLine(blackPen, 20, 50, 200, 50)
            blackPen.StartCap = LineCap.DiamondAnchor
            blackPen.EndCap = LineCap.DiamondAnchor
            g.DrawLine(blackPen, 20, 70, 200, 70)
            blackPen.StartCap = LineCap.Flat
            blackPen.EndCap = LineCap.Flat
            g.DrawLine(blackPen, 20, 110, 200, 110)
            blackPen.StartCap = LineCap.RoundAnchor
            blackPen.EndCap = LineCap.RoundAnchor
            g.DrawLine(blackPen, 20, 130, 200, 130)
            blackPen.StartCap = LineCap.Square
            blackPen.EndCap = LineCap.Square
            g.DrawLine(blackPen, 20, 150, 200, 150)
            blackPen.StartCap = LineCap.SquareAnchor
            blackPen.EndCap = LineCap.SquareAnchor
            g.DrawLine(blackPen, 20, 170, 200, 170)
            blackPen.StartCap = LineCap.Flat
            blackPen.EndCap = LineCap.Flat
            g.DrawLine(blackPen, 20, 190, 200, 190)
            ' Dispose of objects
            blackPen.Dispose()
            g.Dispose()
        End Sub
    End Class
End Namespace
 

The output of Listing 9.2 looks like Figure 9.4, in which the lines have different caps.
 
The LineDashStyle menu item click event handler code is given in Listing 9.3. We create a pen and set the dash style and dash offset values using the DashStyle and DashOffset properties of the Pen object, and then we draw lines.
 
figure-9.4.gif
 
FIGURE 9.4: Reading line caps
 
LISTING 9.3: Getting line dash styles

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Namespace GettingSetting_Line_CapsStyles
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
            Dim g As Graphics = Me.CreateGraphics()
            g.Clear(Me.BackColor)
            ' Create a pen
            Dim blackPen As New Pen(Color.Black, 6)
            ' Set line styles
            blackPen.DashStyle = DashStyle.Dash
            blackPen.DashOffset = 40
            blackPen.DashCap = DashCap.Triangle
            g.DrawLine(blackPen, 20, 10, 500, 10)
            blackPen.DashStyle = DashStyle.DashDot
            g.DrawLine(blackPen, 20, 30, 500, 30)
            blackPen.DashStyle = DashStyle.DashDotDot
            g.DrawLine(blackPen, 20, 50, 500, 50)
            blackPen.DashStyle = DashStyle.Dot
            g.DrawLine(blackPen, 20, 70, 500, 70)
            blackPen.DashStyle = DashStyle.Solid
            g.DrawLine(blackPen, 20, 70, 500, 70)
            ' Dispose of objects
            blackPen.Dispose()
            g.Dispose()
        End Sub
    End Class
End Namespace

 
figure-9.5.gif
 
FIGURE 9.5: Reading line dash styles
 
Figure 9.5 shows the output from Listing 9.3. The lines have different dash styles.
 
The GetCapStyle menu item click event handler code is given in Listing 9.4. We create a pen and set the dash cap styles using the DashCap property of the Pen object.
 
LISTING 9.4: Getting dash caps

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Namespace GettingSetting_Line_CapsStyles
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
            Dim g As Graphics = Me.CreateGraphics()
            g.Clear(Me.BackColor)
            ' Create a pen
            Dim blackPen As New Pen(Color.Black, 10)
            ' Set DashCap styles
            blackPen.DashStyle = DashStyle.DashDotDot
            blackPen.DashPattern = New Single() {10}
            blackPen.DashCap = DashCap.Triangle
            g.DrawLine(blackPen, 20, 10, 500, 10)
            blackPen.DashCap = DashCap.Flat
            g.DrawLine(blackPen, 20, 30, 500, 30)
            blackPen.DashCap = DashCap.Round
            g.DrawLine(blackPen, 20, 50, 500, 50)
            ' Dispose of objects
            blackPen.Dispose()
        End Sub
    End Class
End Namespace

 
Figure 9.6 shows the output from Listing 9.4. The lines have different dash caps: triangular, flat, and round, respectively.
 
figure-9.6.gif
 
FIGURE 9.6: Getting line dash caps
 
HTML clipboard
Conclusion
 
Hope the article would have helped you in understanding Getting and Setting Line Caps and Styles in GDI+. 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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor