【问题标题】:Draw Shape in Windows Form Application在 Windows 窗体应用程序中绘制形状
【发布时间】:2016-03-23 22:23:23
【问题描述】:

我正在尝试绘制一个有 4 个角的形状。拐角细节在 X 和 Y 坐标中给出(如下图所示)。我尝试了此链接给出的方法: Drawing Colors in a picturebox?。但问题是它只适用于矩形。

任何人都可以提出一些建议。我基本上需要它来生成汽车的扫掠路径(汽车行驶时的区域)。所以,我得到 X 和 Y 的汽车中心和度数的方向。由此我确定了汽车在 X 和 Y 空间中的拐角点。现在我需要展示它可视化它。请帮忙。

【问题讨论】:

    标签: c# forms drawing


    【解决方案1】:

    您可以在Form/Control的OnDraw方法中使用Graphics.DrawPolygon(或Graphics.FillPolygon)方法,如下:

    protected override void OnPaint(PaintEventArgs e)
    {
       // If there is an image and it has a location, 
       // paint it when the Form is repainted.
       base.OnPaint(e);
       PointF[] rotatedVertices = // Your rotated rectangle vertices
       e.Graphics.DrawPolygon(yourPen, rotatedVertices);
       // OR
       e.Graphics.FillPolygon(new SolidBrush(Color.Red), rotatedVertices);
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用Rectangle 类和Matrix 类来创建一个矩形,然后按照您的方向旋转它,如下所示:

      Graphics g = new Graphics()
      Rectangle car = new Rectangle(200, 200, 100, 50)
      Matrix m = new Matrix()
      m.RotateAt(orientation, new PointF(car.Left + (car.Width / 2), car.Top + (car.Height / 2)));
      g.Transform = m
      g.FillRectangle(Pens.Red, car)
      

      【讨论】:

        【解决方案3】:

        既然你知道旋转度数,你可以使用Graphics.RotateTransform。这样你就不需要自己计算拐角了(猜猜这个实现会更快)。

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.RotateTransform(45 /* your degrees here */);
            e.Graphics.FillRectangle(Brushes.Red, 10, 10, 200, 100);
        }
        

        请注意,它围绕(0;0) 旋转,因此您可能还需要翻译它(使用Graphics.TranslateTransform)。

        【讨论】:

          猜你喜欢
          • 2017-05-20
          • 2015-07-04
          • 1970-01-01
          • 1970-01-01
          • 2019-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多