How to draw an arrow with DrawLine command
DrawLine in C# (e.g., in WinForms or GDI+) only draws a straight line — it does not support arrows directly. Graphics libraries usually do not support DrawArrow command by default beacuse it is a simple task to mimic DrawArrow command using three DrawLine commands. To draw an arrow, you combine:
• Graphics.DrawLine
• Pen.CustomEndCap (recommended), or
• manual triangle drawing
1. Easiest way: Use CustomEndCap (recommended)
This is the cleanest solution.
using System.Drawing;
using System.Drawing.Drawing2D;
void DrawArrow(Graphics g)
{
Pen pen = new Pen(Color.Black, 2);
// Create arrow shape at the end of the line
AdjustableArrowCap arrowCap = new AdjustableArrowCap(4, 6);
pen.CustomEndCap = arrowCap;
g.DrawLine(pen, 50, 50, 200, 50);
}
What happens
• Line is drawn from (50,50) to (200,50)
• Arrowhead is automatically added at the end
2. Alternative: Use LineCap (simpler, less control)
using System.Drawing;
using System.Drawing.Drawing2D;
void DrawArrow(Graphics g)
{
Pen pen = new Pen(Color.Black, 2);
pen.EndCap = LineCap.ArrowAnchor;
g.DrawLine(pen, 50, 80, 200, 80);
}
Notes
• Very simple
• Less customizable than CustomEndCap
3. Manual arrow (for full control)
If you want full control over shape/angle:
void DrawArrow(Graphics g)
{
Point start = new Point(50, 120);
Point end = new Point(200, 120);
// Draw main line
g.DrawLine(Pens.Black, start, end);
// Draw arrowhead manually
float arrowSize = 10;
var angle = Math.Atan2(end.Y - start.Y, end.X - start.X);
PointF p1 = new PointF(
end.X - (float)(arrowSize * Math.Cos(angle - Math.PI / 6)),
end.Y - (float)(arrowSize * Math.Sin(angle - Math.PI / 6))
);
PointF p2 = new PointF(
end.X - (float)(arrowSize * Math.Cos(angle + Math.PI / 6)),
end.Y - (float)(arrowSize * Math.Sin(angle + Math.PI / 6))
);
g.DrawLine(Pens.Black, end, p1);
g.DrawLine(Pens.Black, end, p2);
}
Another implementation of DrawArrow
The following method draws an arrow using three DrawLine calls in C# programming language.
void DrawArrow(double x1, double y1, double x2, double y2, int arrLength = 10)
{
var m = x2 - x1 == 0 ? 0 : (y2 - y1) / (x2 - x1);
var degree = Math.Atan(m);
var toLeft = x2 > x1 ? 0 : Math.PI;
var degree1 = degree + 5 * Math.PI / 6 + toLeft;
var degree2 = degree + 7 * Math.PI / 6 + toLeft;
var px1 = x2 + Math.Cos(degree1) * arrLength;
var py1 = y2 + Math.Sin(degree1) * arrLength;
var px2 = x2 + Math.Cos(degree2) * arrLength;
var py2 = y2 + Math.Sin(degree2) * arrLength;
var pen = new Pen(Color.Black);
g.DrawLine(pen, x1, y1, x2, y2);
g.DrawLine(pen, x2, y2, px1, py1);
g.DrawLine(pen, x2, y2, px2, py2);
}