Search code examples
c#winformsgdi+system.drawing

Drawing a linkage or a path between two lines using c#


I am trying to draw a path between 2 lines like the following one.

wanted graph

I used the following code to do that

        Pen usedpen= new Pen(Color.Black, 2);
        //Point[] p = {
        //    new Point(518,10),
        //    new Point(518,20),
        //    new Point(518-85,15)
        //};
        GraphicsPath path = new GraphicsPath();

        path.StartFigure();
        path.AddLine(new Point(518, 10), new Point(433, 10));
        path.AddLine(new Point(518, 40), new Point(433, 40));
        path.AddLine(new Point(433,10), new Point(433,40));
        //usedpen.LineJoin = LineJoin.Round;
        e.Graphics.DrawPath(usedpen, path);

But after use this code the following graphic is drawn:

Result

Any help
Thanks in advance


Solution

  • Oh you're using the onPaint event, so the problem is You're drawing a path which means the Point will go from end of the first line to the starting of the next line.

    After the first line

    path.AddLine(new Point(518, 10), new Point(433, 10));

    Now the Point is at (433, 10)

    Now the next line Says go from (518, 40) to (433, 40)

    now what's actually happening is there's a line being drawn from (433, 10) to (518, 40) because it's a path it continue drawing.

     GraphicsPath path = new GraphicsPath();
     path.StartFigure();
     path.AddLine(new Point(518, 10), new Point(433, 10));
     path.AddLine(new Point(433, 10), new Point(433, 40));
     path.AddLine(new Point(433, 40), new Point(518, 40));
     usedpen.LineJoin = LineJoin.Round;