Stroke Caps in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

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.

Figure 9.12.jpg

FIGURE 9.12: The Round line join effect
Figure 9.13.jpg
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

private void SetStrokesCapsMenu_Click(object sender, System.EventArgs e)
        {
            // Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.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).
            Point[] points =
            {
            new Point (-3, -3),
            new Point (0,0),
            new Point (3, -3)
            };

            GraphicsPath path = new GraphicsPath();
            path.AddLines(points);

            // Create a custom line cap from the path
            CustomLineCap cap = new CustomLineCap(null, 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
            Pen redPen = 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();
        }

Figure 9.14 shows the output from Listing 9.7.

Figure 9.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.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all