How to: Draw a Line with Line Caps

You can draw the start or end of a line in one of several shapes called line caps. GDI+ supports several line caps, such as round, square, diamond, and arrowhead.

Example

You can specify line caps for the start of a line (start cap), the end of a line (end cap), or the dashes of a dashed line (dash cap).

The following example draws a line with an arrowhead at one end and a round cap at the other end. The illustration shows the resulting line:

Pens

Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255), 8)
pen.StartCap = LineCap.ArrowAnchor
pen.EndCap = LineCap.RoundAnchor
e.Graphics.DrawLine(pen, 20, 175, 300, 175)
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 255), 8);
pen.StartCap = LineCap.ArrowAnchor;
pen.EndCap = LineCap.RoundAnchor;
e.Graphics.DrawLine(pen, 20, 175, 300, 175);

Compiling the Code

  • Create a Windows Form and handle the form's Paint event. Paste the example code into the Paint event handler passing e as PaintEventArgs.

See Also

Reference

System.Drawing.Pen
System.Drawing.Drawing2D.LineCap

Other Resources

Graphics and Drawing in Windows Forms
Using a Pen to Draw Lines and Shapes