【问题标题】:Using Graphics to load an image and rotate it into a panel使用 Graphics 加载图像并将其旋转到面板中
【发布时间】:2012-03-09 03:06:16
【问题描述】:

我正在尝试使用图形对象来加载图像,然后将其旋转(纵向或横向),然后将其显示在面板中(不是图片框)。

如何在面板中加载图形?另外,在图形对象上进行横向或纵向旋转的最简单方法是什么?

必须使用 GDI 来旋转和处理图像,我需要一种将 Graphics 对象放入面板的方法。

【问题讨论】:

  • Gtk-Sharp?开罗?随心所欲? OpenGL? WebkitSharp?你需要更多的标签
  • 已更新,感谢您指出这一点。

标签: c# winforms gdi


【解决方案1】:

使用Panel的Paint事件:

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        int angle = 90;
        Graphics g = e.Graphics;
        Image i = new Bitmap(@"C:\Jellyfish.jpg");
        g.TranslateTransform((float)i.Width / 2, (float)i.Height / 2);
        g.RotateTransform(angle);
        g.TranslateTransform(-(float)i.Width / 2, -(float)i.Height / 2);
        g.DrawImage(i, new Point(0,0));

    }

【讨论】:

  • 这不是在旋转时裁剪原始图像吗?
【解决方案2】:

既然您说的是面板而且它是 C#,我猜您指的是 WinForms。

您可以使用RotateFlip 方法旋转任何Image 实例,并且可以使用Image 作为面板的BackgroundImage。一个工作示例:

Bitmap bitmap = new Bitmap(@"D:\word.png");
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
Form form = new Form() { Height = 400, Width = 600 };
Panel p = new Panel() { Height = 400, Width = 600, Left = 0, Top = 0};
form.Controls.Add(p);
p.BackgroundImage = bitmap;
form.Show();

【讨论】:

  • 谢谢,虽然我使用 GDI 处理图像很重要 :)
  • 你的意思是你不能使用RotateFlip方法吗?
  • 不,我目前正在使用您建议的方法,但要求我使用 GDI 来处理图像。旋转(纵向或横向)后,需要将其加载到面板中。我的意思是说 GDI 而不是 RotateFlip 方法。
猜你喜欢
  • 2016-08-09
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多