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.


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:


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);
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)

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

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