Getting and Setting Line Caps and Styles in GDI+


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

We create a Windows application 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

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

namespace GettingSetting_Line_CapsStyles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            // Create a pen
            Pen blackPen = 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();
        }
    }
}

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

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

namespace GettingSetting_Line_CapsStyles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            // Create a pen
            Pen blackPen = 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();
        }
    }
}

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

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

namespace GettingSetting_Line_CapsStyles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            // Create a pen
            Pen blackPen = new Pen(Color.Black, 10);
            // Set DashCap styles
            blackPen.DashStyle = DashStyle.DashDotDot;
            blackPen.DashPattern = new float[] { 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();
        }
    }
}

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

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.

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.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.