How to rotate image in picture box

前端 未结 2 887
温柔的废话
温柔的废话 2020-12-04 02:18

I am making a winforms application. One of the features I hope to implement is a rotating gear on the home form.

When the home form is loaded, you should hover over

2条回答
  •  无人及你
    2020-12-04 03:01

    You'll have to use Timer to create rotation of the Image. There is no built in method exists for rotation.

    Create a global timer:

    Timer rotationTimer;
    

    Initialize timer in the constructor of the form and create PictureBox MouseEnter and MouseLeave events:

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

    Then create their 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();
    }
    

提交回复
热议问题