Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a dashed line over an object?

I am drawing a line on a control on my Windows form like this:

            // Get Graphics object from chart
            Graphics graph = e.ChartGraphics.Graphics;

            PointF point1 = PointF.Empty;
            PointF point2 = PointF.Empty;

            // Set Maximum and minimum points
            point1.X = -110;
            point1.Y = -110;
            point2.X = 122;
            point2.Y = 122;

            // Convert relative coordinates to absolute coordinates.
            point1 = e.ChartGraphics.GetAbsolutePoint(point1);
            point2 = e.ChartGraphics.GetAbsolutePoint(point2);

            // Draw connection line
            graph.DrawLine(new Pen(Color.Yellow, 3), point1, point2);

I would like to know if it is possible to draw a dashed (dotted) line instead of a regular solid line?

like image 612
Alex Gordon Avatar asked May 23 '11 17:05

Alex Gordon


People also ask

How do I make a dashed line in Python?

Dot() function is used to make a dotted line.


5 Answers

It's pretty simple once you figure out the formatting that defines the dashes:

float[] dashValues = { 5, 2, 15, 4 };
Pen blackPen = new Pen(Color.Black, 5);
blackPen.DashPattern = dashValues;
e.Graphics.DrawLine(blackPen, new Point(5, 5), new Point(405, 5));

The numbers in the float array represent dash lengths of different colors. So for a simple dash of 2 pixels on (black) and two off each your aray would look like: {2,2} The pattern then repeats. If you wanted 5-wide dashes with a space of 2 pixels you would use {5,2}

In your code it would look like:

// Get Graphics object from chart
Graphics graph = e.ChartGraphics.Graphics;

PointF point1 = PointF.Empty;
PointF point2 = PointF.Empty;

// Set Maximum and minimum points
point1.X = -110;
point1.Y = -110;
point2.X = 122;
point2.Y = 122;

// Convert relative coordinates to absolute coordinates.
point1 = e.ChartGraphics.GetAbsolutePoint(point1);
point2 = e.ChartGraphics.GetAbsolutePoint(point2);

// Draw (dashed) connection line
float[] dashValues = { 4, 2 };
Pen dashPen= new Pen(Color.Yellow, 3);
dashPen.DashPattern = dashValues;
graph.DrawLine(dashPen, point1, point2);
like image 167
Paul Sasik Avatar answered Oct 02 '22 19:10

Paul Sasik


I think you can accomplish this by changing the pen you use to draw your line. So, replace the last 2 lines in your example with:

        var pen = new Pen(Color.Yellow, 3);
        pen.DashStyle = DashStyle.Dash;
        graph.DrawLine(pen, point1, point2);
like image 35
Gustavo Mori Avatar answered Oct 02 '22 17:10

Gustavo Mori


Pen has a public property that is defined as

public DashStyle DashStyle { get; set; }

you can set DasStyle.Dash if you want to draw a Dashed line.

like image 26
crypted Avatar answered Oct 02 '22 18:10

crypted


Pen.DashPattern will do this. Look here for an example

like image 41
Emond Avatar answered Oct 02 '22 19:10

Emond


In more modern C#:

var dottedPen = new Pen(Color.Gray, width: 1) { DashPattern = new[] { 1f, 1f } };
like image 40
Drew Noakes Avatar answered Oct 02 '22 17:10

Drew Noakes