BLOG.CSHARPHELPER.COM: Draw lines with custom dash patterns in C#
Draw lines with custom dash patterns in C#
To create a dashed line, first create a Pen. Then set the Pen's DashStyle property.
For example, the following code draws two lines, one with DashStyle Dash and one with DashStyle DashDot.
using (Pen dashed_pen = new Pen(Color.Blue, 2)) { ... dashed_pen.DashStyle = DashStyle.Dash; e.Graphics.DrawString("Dash", this.Font, Brushes.Black, 10, y - 8); e.Graphics.DrawLine(dashed_pen, 100, y, 250, y); y += 20;
dashed_pen.DashStyle = DashStyle.DashDot; e.Graphics.DrawString("DashDot", this.Font, Brushes.Black, 10, y - 8); e.Graphics.DrawLine(dashed_pen, 100, y, 250, y); y += 20; ... }
Don't forget to use the Pen's Dispose method to free graphics resources. If you use the using statement as in this example, C# calls Dispose automatically for you.
Comments