2014-10-19 78 views
3

我正在制作一个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

+2

最简单的方法可能是创建一个动画GIF,让PictureBox的为你做 – Plutonix 2014-10-19 12:58:35

+0

工作这是相当简单的绘制图像(而不是设置它)和转换图形.. – TaW 2014-10-19 13:05:21

+0

你可能想看看[我的例子](http://stackoverflow.com/a/14711744/643085)这样的一个使用当前的,不推荐的.Net Windows UI技术的东西,w这不需要愚蠢的“所有者抽取”黑客之类的事情,并将这项任务简化为仅有的两行DataBinding事物。 – 2014-10-20 01:47:59

回答

4

您将不得不使用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(); 
} 
3

你可以使用这样的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 
} 

确保图像是正方形,并在中间的齿轮!这里是一个很好的一个:

enter image description hereenter image description heregear with 15 cogs

这是一个无闪烁doublebuffered面板:

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

也是很好的答案。感谢照片。我努力在网上找到,我喜欢 – 2014-10-20 05:53:42

+1

祝你好运。由于您使用的是Shaharyar的解决方案,该解决方案并不真正旋转,只是简单地翻转90°,所以您必须搜索具有这些大增量的看起来不错的装备。上述图像具有60°对称性(实际上已折断)被翻转时看起来不太好..齿轮的数量应该可以被3整除,但不能被2或4整除。我添加一个有15个齿轮的齿轮。 – TaW 2014-10-20 12:31:33

+0

感谢您的建议 – 2014-10-21 06:02:49