2014-10-19 72 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