c#旋转嵌入的图像

时间:2015-09-23 21:55:49

标签: c# image graphics

我有一个图像加载.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);

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

如果将该程序语句更改为:

,您可能会得到更接近所需结果的结果
i

其他问题包括:

  • 完成后g.DrawImage(a, new Rectangle(new Point(229, 120), new Size(a.Width, a.Height))); ga未能处理
  • 在旋转后未能将g翻译回所需位置(即您翻译以使图像的旋转发生在图像的点(21,110)周围,但已将图像保留为已翻译轮换后的金额,可能不是你想要的
  • 对前面提到的翻译使用正偏移量

由于这些问题(可能还有其他问题),我不认为上述变化会产生所需的输出,但至少你会看到一些轮换的证据。

虽然您的错误似乎不是我在the question commenter Keith M mentioned的答案中解决的错误,但我确实认为您可以从阅读代码示例中受益,因为它基本上与您完全相同正试图在这里做。