【问题标题】:Graphics flip the canvas vertically图形垂直翻转画布
【发布时间】:2016-10-15 17:54:13
【问题描述】:

我从一个名为line 的列表中放置坐标,用我的panel1 缩放它们并使用GraphicsPath 绘制连接它们的线。我遇到的问题是图像垂直翻转,可能是由于 panel1 的 (0,0) 坐标位于左上角,而不是我的正常坐标源自左下角为零的坐标系。代码如下:

 Graphics G = e.Graphics;
 GraphicsPath gp = new GraphicsPath();
 foreach (var line in tockeKoordinate)
 {
     gp.AddLine((float)(line.startX), (float)(line.startY),
                (float)(line.endX),   (float)(line.endY));
     gp.CloseFigure();
 }
 var rect = gp.GetBounds();
 var scale = Math.Min(1f * (int)(panel1.ClientSize.Width)  / rect.Width,
                      1f * (int)(panel1.ClientSize.Height) / rect.Height);

 using (Pen pen = new Pen(Color.DarkGreen, 0.0001f))
 {
     G.SmoothingMode = SmoothingMode.AntiAlias;
     G.Clear(Color.White);
     G.ScaleTransform(scale, scale);
     G.TranslateTransform(-rect.X, -rect.Y);
     G.DrawPath(pen, gp);
 }

我一直在搜索,不知何故与 G.TranslateTransform 行有关,但在值中添加减号前缀的成功率为零...

【问题讨论】:

  • 您真的应该只使用 (0, 0) 位于左上角的正常图形约定,而不是从数学约定中执行它然后稍后翻转。
  • 请参阅here 如何使用翻转的图形对象。请注意,您需要知道画布的大小!
  • @Jashaszun 源 GPS 坐标是这样的,而且很多坐标都无法手动更改。
  • 您可以在将它们添加到 GraphicsPath 之前更改它们。
  • @TaW 最终解决了,就像您在链接解决方案中所说的那样,顺序确实很重要 :)

标签: c# graphics panel coordinate-systems


【解决方案1】:

在 TaW 和 James Lambert 的帮助下解决了。需要通过取反 ScaleTransform 的 Y 参数来翻转轴,还需要使用 TranslateTransform 将画布向下:

  G.TranslateTransform(0, +panel1.ClientSize.Height);
  G.ScaleTransform(scale, -scale);

【讨论】:

    【解决方案2】:

    TranslateTransform 只是移动东西,但不能翻转任何东西。您可以通过对 ScaleTransform 的参数之一取反来翻转它。

    【讨论】:

    • 试过了,屏幕就黑了。
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 2013-11-18
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多