【问题标题】:c# rotate embedded imagec# 旋转嵌入图像
【发布时间】:2015-09-23 21:55:48
【问题描述】:

我有一个加载 .PNG 文件的图像,然后将它与我创建的其他图形结合起来。问题是我只需要旋转 .PNG 文件,而不是整个文件。想象一个速度计,你有一个从 0 到 200 的背景图像。该图像始终保持静止。现在,在它之上,你有一个指向你当前速度的箭头。那就是我要旋转的那个。

这是我目前所拥有的。它确实显示图形,但不旋转 .PNG(箭头)

        Bitmap bitmap = new Bitmap(500, 280, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bitmap);
        g.Clear(Color.White);

        //this.Arrow = path to the .PNG
        Image i = Image.FromFile(this.Arrow);
        Bitmap a = new Bitmap(i.Width, i.Height);
        Graphics ga = Graphics.FromImage(a);
        a.SetResolution(ga.DpiX, ga.DpiY);

        //It shouldn't rotate having the pivot at the (centre, centre)
        //coordinates, but at the bottom of the image. 
        //The (21, 110) coordinates are right relative to the .PNG file
        ga.TranslateTransform(21, 110);
        ga.RotateTransform(45);   //<--- Not rotating
        ga.DrawImage(i, 0, 0);

        g.DrawImage(i, new Rectangle(new Point(229, 120), new Size(i.Width, i.Height)));

        g.DrawLine(new Pen(new SolidBrush(Color.Aquamarine), 1), 250, 0, 250, 280);
        g.DrawLine(new Pen(new SolidBrush(Color.Aquamarine), 1), 0, 230, 500, 230);

有什么想法吗?

【问题讨论】:

  • 您的问题可能与其他问题的第一个答案有关:stackoverflow.com/questions/27996037/…
  • 它还没有工作。甚至尝试使用 Matrix() 类和 .Transform 属性,但结果仍然相同。

标签: c# image graphics


【解决方案1】:

如果没有可靠地重现问题的a good, minimal, complete code example,就不可能确定问题是什么。您发布的代码中可能存在几个问题;最明显的是,您将图像 i 绘制到 Graphics 对象 g,而不是图像 a 中,而 i 以 45 度旋转。

如果您将该程序语句更改为以下内容,您可能会获得更接近预期的结果:

g.DrawImage(a, new Rectangle(new Point(229, 120), new Size(a.Width, a.Height)));

其他问题包括:

  • 在处理完 gag 后未能处理它们
  • 在旋转后未能将ga 转换回所需位置(即,您进行平移以使图像的旋转发生在图像的点 (21, 110) 周围,但图像仍由该点平移旋转后的数量,这可能不是你想要的
  • 对前面提到的平移使用正偏移

由于这些问题(可能还有其他问题),我认为上述更改不会产生完全理想的输出,但至少您会看到一些轮换的证据。

虽然您的错误似乎不是我在对the question commenter Keith M mentioned 的回答中提到的,但我认为您会从阅读那里的代码示例中受益,因为它与您尝试做的事情基本相同在这里。

【讨论】:

  • 发布了良好的最小代码示例。这就是我正在使用的。
  • 请阅读我提供的链接,以便您了解“好、最小、完整代码示例”的含义。 “最小”是必要条件,但不是充分条件。您的示例也需要完整。另外,请考虑阅读答案并尝试建议的解决方案,然后再将其关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2022-07-11
  • 2015-07-20
  • 2011-01-17
  • 2018-10-14
相关资源
最近更新 更多