Comment faire pivoter l’image dans la boîte d’image

Je fais une application winforms. L’une des fonctionnalités que j’espère mettre en oeuvre est un engrenage rotatif sur la forme maison.

Lorsque le formulaire d’origine est chargé, passez la souris sur l’image du mécanisme et faites-la pivoter.

Mais tout ce que j’ai jusqu’à présent, c’est le RotateFlip, qui retourne tout simplement l’image.

Existe-t-il un moyen de faire tourner l’engrenage lorsque la souris le survole?

Le code que j’ai jusqu’à présent est:

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; } 

Comme je l’ai dit, je veux juste passer à la vitesse supérieure. J’essaie de le faire dans une application Windows Form. Utilisation de C #. Cadre 4

Vous devrez utiliser le Timer pour créer une rotation de l’ Image . Il n’y a pas de méthode intégrée pour la rotation.

Créer une timer globale:

 Timer rotationTimer; 

Initialisez timer dans le constructeur du formulaire et créez les MouseEnter PictureBox MouseEnter et MouseLeave :

 //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; 

Puis créez leurs 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(); } 

Vous pouvez utiliser la méthode Graphics.RotateTransform comme ceci; J’utilise un panneau double tampon, un minuteur et deux variables de classe.

 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 } 

Assurez-vous que l’image est carrée et que l’engrenage est au centre !! En voici une belle:

entrez la description de l'image icientrez la description de l'image icivitesse avec 15 rouages

Voici un panneau à double tampon sans scintillement:

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