如何旋转图片框图像 [英] How to rotate image in picture box

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

问题描述

我想提出一个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?

我到目前为止的代码是:

The code I have so far is:

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

推荐答案

您将不得不使用定时以创建旋转在图像。有在方法没有内置存在转动

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;



初始化计时器,并创建图片框 的MouseEnter 鼠标离开事件:

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



然后建立自己的事件句柄

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆