BLOG.CSHARPHELPER.COM: Use base insets with custom end caps in C#
Use base insets with custom end caps in C#
The example Draw lines with custom end caps in C# shows how to draw lines with custom end caps. For example, using those techniques you can draw lines with arrowheads or circles at the ends.
The coordinate system for drawing end caps has Y increasing in the direction of the line so, if you draw an end cap that uses a positive Y coordinate, the end cap sticks out beyond the line's normal end point. If you don't want it to, you can move the end cap's shape back so it doesn't use positive Y coordinate but then it may draw on top of the line itself. If the end cap should be a circle, rectangle, or other open shape, that may look strange.
You can fix this problem by using the CustomLineCap class's BaseInset property. This value indicates the distance short by which the line should end, measured in line thicknesses as usual.
This example draws lines with a Pen that draws a circle around the line's starting point and a dot at the line's ending point. In the top of the picture, the CustomLineCap objects don't use BaseInset so the line extends into the center of the circle at the line's beginning and extends until it touches the point at the line's end.
After drawing the three upper lines, the program sets BaseInset to 2 for both end caps so the line stops just touching the circle at the beginning and with a small gap between the line and the point at the end.
The following code shows the program's Paint event handler, which creates the Pen and draws the lines.
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (Pen the_pen = new Pen(Color.Blue, 5))
{
// Make a GraphicsPath to define the start cap.
using (GraphicsPath start_path = new GraphicsPath())
{
start_path.AddEllipse(-2, -2, 4, 4);
// Make the start cap.
using (CustomLineCap start_cap = new CustomLineCap(null, start_path))
{
// Make the end cap.
using (GraphicsPath end_path = new GraphicsPath())
{
end_path.AddEllipse(-0.5f, -0.5f, 1, 1);
// Make the start cap.
using (CustomLineCap end_cap = new CustomLineCap(end_path, null))
{
the_pen.CustomStartCap = start_cap;
the_pen.CustomEndCap = end_cap;
// Draw some lines without base insets.
the_pen.Color = Color.Green;
e.Graphics.DrawLine(the_pen, 20, 20, 100, 60);
the_pen.Color = Color.Orange;
e.Graphics.DrawLine(the_pen, 100, 60, 160, 30);
the_pen.Color = Color.Blue;
e.Graphics.DrawLine(the_pen, 160, 30, 250, 70);
// Translate and draw with insets.
e.Graphics.TranslateTransform(0, 50);
start_cap.BaseInset = 2;
end_cap.BaseInset = 2;
the_pen.CustomStartCap = start_cap;
the_pen.CustomEndCap = end_cap;
the_pen.Color = Color.Green;
e.Graphics.DrawLine(the_pen, 20, 20, 100, 60);
the_pen.Color = Color.Orange;
e.Graphics.DrawLine(the_pen, 100, 60, 160, 30);
the_pen.Color = Color.Blue;
e.Graphics.DrawLine(the_pen, 160, 30, 250, 70);
}
}
}
}
}
}
Comments