Stroke Caps in GDI+ using VB.NET

In this article I will explain about Stroke Caps in GDI+.
  • 3887
We have already seen how to use the StartCap and EndCap properties of a Pen object to set the starting and ending caps of lines. We have also seen how to use the StartCustomCap and EndCustomCap properties to set customized starting and ending caps.

 
Figure209_12.jpg
 
  
FIGURE 9.12: The Round line join effect

Untitled.png

 
FIGURE 9.13: Customized starting and ending caps

To understand caps better, take a look at Figure 9.13. The rectangle A is a line cap. The starting cap is triangular and the ending cap is round.
 
The GetStrokeCaps and SetStrokeCaps methods of the CustomLineCap class can also be used to get and set the starting and ending caps of a custom cap. The SetStrokeCaps method takes two arguments of type LineCap enumeration and sets the caps for the starting and ending points of lines. Listing 9.7 creates custom line caps and sets them using the SetStrokeCaps methods. After creating custom line caps, we create a pen and set its CustomStartCap and CustomEndCap properties, which use the pen to draw a line.
 
LISTING 9.7: Using SetStrokeCaps

Public Class Form1

    Private Sub SetStrokesCapsMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Create a Graphics object
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)

        ' Create a path for custom line cap. This path will have two line from points
        ' (-3, -3) to (0, 0) and (0, 0) to (3, -3).
        Dim points As Point() = {New Point(-3, -3), New Point(0, 0), New Point(3, -3)}

        Dim path As New GraphicsPath()
        path.AddLines(points)

        ' Create a custom line cap from the path
        Dim cap As New CustomLineCap(Nothing, path)

        ' Set the starting and ending caps of the custom cap
        cap.SetStrokeCaps(LineCap.Round, LineCap.Triangle)

        ' Create a Pen object and set its starting and ending caps
        Dim redPen As New Pen(Color.Red, 15)
        redPen.CustomStartCap = cap
        redPen.CustomEndCap = cap
        redPen.DashStyle = DashStyle.DashDotDot

        ' Draw the line
        g.DrawLine(redPen, New Point(100, 100), New Point(400, 100))
        ' Dispose of objects
        g.Dispose()
    End Sub
    End Sub
End Class
 

Figure 9.14 shows the output from Listing 9.7.
 
Figure209_14.jpg
 
FIGURE 9.14: setting customized starting and ending caps
 
Conclusion

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.