17

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?

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

6 Answers6

38

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);
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • thanks so much so can you show me how i would incorporate this into my code – Alex Gordon May 23 '11 at 17:24
  • @I: plz see my edit. Note of caution though. I'm not running this through a compiler so there may be syntax and intent errors. In any case, should get you close. – Paul Sasik May 23 '11 at 17:35
11

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);
Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
7

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.

crypted
  • 10,118
  • 3
  • 39
  • 52
3

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

Emond
  • 50,210
  • 11
  • 84
  • 115
2

In more modern C#:

var dottedPen = new Pen(Color.Gray, width: 1) { DashPattern = new[] { 1f, 1f } };
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
0

To answer this question regarding the generation of a dashed line using the code-behind:

        Pen dashPenTest = new(Brushes.DodgerBlue, 1);

        Line testLine = new()
        {
            Stroke = dashPenTest.Brush, //Brushes.Aqua,
            StrokeThickness = dashPenTest.Thickness,//1,
            StrokeDashArray = new DoubleCollection() { 8,4 },
            X1 = 0,
            X2 = canvas.Width,
            Y1 = 10,
            Y2 = 10
        }; 
        canvas.Children.Add(testLine);

This answer make use of the generation of a canvas in the xaml:

        <Canvas x:Name ="canvas" Background="White" Height="300" Width="300">

The important method here is the "StrokeDashArray" that generates the dashes for the line drawn. More information is given here: Shape.StrokeDashArray