【问题标题】:How to rotate image in picture box如何旋转图片框中的图像
【发布时间】:2014-10-19 12:57:47
【问题描述】:

我正在制作一个 winforms 应用程序。我希望实现的功能之一是主窗体上的旋转齿轮。

加载主页表单后,您应该将鼠标悬停在齿轮的图片上,并且它应该会旋转到位。

但到目前为止,我只有 RotateFlip,它只是翻转图片。

当鼠标悬停在齿轮上时,有没有办法让齿轮转动到位?

我目前的代码是:

Bitmap bitmap1;
    public frmHome()
    {
        InitializeComponent();
        try
        {
            bitmap1 = (Bitmap)Bitmap.FromFile(@"gear.jpg");
            gear1.SizeMode = PictureBoxSizeMode.AutoSize;
            gear1.Image = bitmap1;
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("There was an error." +
                "Check the path to the bitmap.");
        }
    }

    private void frmHome_Load(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
    }

    private void frmHome_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }

    private void pictureBox1_MouseHover(object sender, EventArgs e)
    {

        bitmap1.RotateFlip(RotateFlipType.Rotate180FlipY);
        gear1.Image = bitmap1;
    }

就像我说的,我只想转动齿轮。我正在尝试在 Windows 窗体应用程序中执行此操作。使用 C#。框架 4

【问题讨论】:

  • 最简单的方法可能是创建一个动画 GIF 并让图片框为您完成工作
  • 通过绘制图像(而不是设置它)并转换图形非常简单..
  • 您可能想看看my example 这样的东西,它使用当前的、未弃用的.Net Windows UI 技术,它消除了对愚蠢的“所有者绘制”黑客等的需要,并将此任务简化为仅 2 行 DataBinding 的事情。

标签: c# winforms rotation picturebox image-rotation


【解决方案1】:

您必须使用Timer 来创建Image 的旋转。没有内置的旋转方法。

创建一个全局计时器:

Timer rotationTimer;

在表单的构造函数中初始化定时器并创建PictureBoxMouseEnterMouseLeave事件:

//initializing timer
rotationTimer = new Timer();
rotationTimer.Interval = 150;    //you can change it to handle smoothness
rotationTimer.Tick += rotationTimer_Tick;

//create pictutrebox events
pictureBox1.MouseEnter += pictureBox1_MouseEnter;
pictureBox1.MouseLeave += pictureBox1_MouseLeave;

然后创建他们的Event Handlers:

void rotationTimer_Tick(object sender, EventArgs e)
{
    Image flipImage = pictureBox1.Image;
    flipImage.RotateFlip(RotateFlipType.Rotate90FlipXY);
    pictureBox1.Image = flipImage;
}

private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
    rotationTimer.Start();
}

private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
    rotationTimer.Stop();
}

【讨论】:

    【解决方案2】:

    您可以像这样使用Graphics.RotateTransform 方法;我使用一个双缓冲面板、一个计时器和两个类变量..

    Bitmap bmp;
    float angle = 0f;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        bmp = new Bitmap(yourGrarImage);
        int dpi = 96;
        using (Graphics G = this.CreateGraphics()) dpi = (int)G.DpiX;
        bmp.SetResolution(dpi, dpi);
        panel1.ClientSize = bmp.Size;
    }
    
    private void timer1_Tick(object sender, EventArgs e)
    {
    
        angle+=2;              // set the speed here..
        angle = angle % 360;
        panel2.Invalidate();
    }
    
    
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (bmp!= null) 
        {
                float bw2 = bmp.Width / 2f;    // really ought..
                float bh2 = bmp.Height / 2f;   // to be equal!!!
                e.Graphics.TranslateTransform(bw2, bh2);
                e.Graphics.RotateTransform(angle);
                e.Graphics.TranslateTransform(-bw2, -bh2);
                e.Graphics.DrawImage(bmp, 0, 0);  
                e.Graphics.ResetTransform();
        }
    }
    
    private void panel1_MouseLeave(object sender, EventArgs e)
    {
        timer1.Stop();
    }
    
    private void panel1_MouseHover(object sender, EventArgs e)
    {
        timer1.Start();
        timer1.Interval = 10;    // ..and/or here
    }
    

    确保图片是方形的,中间有齿轮!!这是一个不错的选择:

    这是一个无闪烁双缓冲面板:

    public class Display : Panel
    {
       public Display()
       {
          this.DoubleBuffered = true;
       }
    }
    

    更新: 可以使用 PictureboxLabel(与 Autosize=false );两者都具有开箱即用的DoubleBuffered 属性,并且比Panels 更支持绘图。

    【讨论】:

    • 也不错。谢谢你的照片。我很难在网上找到我喜欢的
    • 祝你好运。由于您使用的是 Shaharyar 的解决方案,它并没有真正旋转,而是简单地翻转 90°,因此您必须寻找一个看起来不错的具有这些大增量的齿轮。上面的图像具有 60° 对称性(实际上打破了几乎没有图案覆盖。)这些在翻转时看起来不太好。。齿轮的数量应该能被 3 整除,但不能被 2 或 4 整除。我添加一个有 15 个 cog 的 ..
    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多