【问题标题】:Drawing a linkage or a path between two lines using c#使用 c# 在两条线之间绘制链接或路径
【发布时间】:2018-10-06 07:28:41
【问题描述】:

我正在尝试在两条线之间绘制一条路径,如下所示。

我使用以下代码来做到这一点

        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);

但是使用这段代码后,绘制了以下图形:

任何帮助
提前致谢

【问题讨论】:

  • 你用什么做画布?
  • 我不使用画布

标签: c# winforms gdi+ system.drawing


【解决方案1】:

哦,您正在使用 onPaint 事件,所以问题是您正在绘制一条路径,这意味着该点将从第一行的末尾到下一行的开头。

第一行之后

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

现在点在 (433, 10)

现在下一行说从 (518, 40) 到 (433, 40)

现在实际发生的情况是从 (433, 10) 到 (518, 40) 绘制了一条线,因为它是一条继续绘制的路径。

 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;

【讨论】:

  • 如果解释混乱,请见谅
  • 不,我理解你的解释,它有效,谢谢兄弟
【解决方案2】:

默认情况下,无论何时添加一个图形,比如一个 Line 到 GraphicPath,它的起点将连接到最后一个图形的终点。因此顺序很重要!

防止这种情况的方法是在您想要绘制未连接线时使用StartFigure

在不关闭当前图窗的情况下开始一个新图窗。随后的所有 添加到路径的点将添加到这个新图形中。

    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.StartFigure();   // <---
    path.AddLine(new Point(518, 40), new Point(433, 40));
    path.StartFigure();   // <---
    path.AddLine(new Point(433,10), new Point(433,40));
    //usedpen.LineJoin = LineJoin.Round;
    e.Graphics.DrawPath(usedpen, path);

当然你的问题基本上是错误的顺序或画线,所以另一个答案是正确的。

顺便说一句,推荐的绘制连接线的方法是使用AddLines,因为它可以避免线接头、帽或斜接处出现问题..

【讨论】:

    【解决方案3】:

    一个类似的方法,使用GraphicsPath.AddLines()(也处理创建的对象)。

    GraphicsPath.StartFigure() 没有被使用,因为它是唯一绘制的图形。当更多点被添加到 GraphicsPath 时,.StartFigure() 将创建一个新的子路径,而不链接预先存在的子路径。

    可以使用GraphicsPathIterator 解析子路径,以测试它们包含的形状类型。

    using (GraphicsPath path = new GraphicsPath())
    {
        Point[] points = new Point[] {
            new Point(518, 10), new Point(433, 10),
            new Point(433, 40), new Point(433, 40),
            new Point(433, 40), new Point(518, 40)
        };
    
        path.AddLines(points);
        e.Graphics.DrawPath(new Pen(Color.Black, 2), path);
    
        //Add a new figure
        path.StartFigure();
        path.AddEllipse(new Rectangle(450, 40, 50, 50));
        e.Graphics.DrawPath(new Pen(Color.Black, 2), path);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多