如何旋转图片框中的图像

How to rotate image in picture box(如何旋转图片框中的图像)

本文介绍了如何旋转图片框中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 the picture of the gear, and it should rotate in place.

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

But all I have so far is the RotateFlip and that just flips the picture.

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

Is there a way to make the gear turn in place when the mouse is hovering over it?

我目前的代码是:

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

Like I said, I just want to turn the gear. I am trying to do this in a Windows Form application. Using C#. Framework 4

推荐答案

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

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

创建一个全局计时器:

Timer rotationTimer;

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

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;

然后创建他们的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();
}

这篇关于如何旋转图片框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何旋转图片框中的图像

基础教程推荐