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
MouseEnter
和MouseLeave
事件:
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();
}
这篇关于如何旋转图片框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何旋转图片框中的图像


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01