C#每点击按钮一次,将picturebox1中的图片顺时针选择90度后显示
时间: 2024-10-24 09:14:30 浏览: 37
在C#中,你可以通过事件处理和图形旋转功能来实现这个功能。首先,你需要设置PictureBox(PictureBox1)有一个Click事件,每次点击按钮时触发。然后,在该事件处理器中,对PictureBox1的Image属性应用图像旋转。
以下是简单的步骤:
1. 定义一个Button控件,并设置其Click事件:
```csharp
private void button_Click(object sender, EventArgs e)
{
RotateImage();
}
```
2. 实现RotateImage()方法,这里使用Graphics类的RotateTransform方法:
```csharp
private void RotateImage()
{
if (pictureBox1.Image != null)
{
// 获取PictureBox1当前的图片
Image originalImage = pictureBox1.Image;
// 创建一个新的Bitmap对象用于存储旋转后的图片
Bitmap rotatedImage = new Bitmap(originalImage.Width, originalImage.Height);
using (Graphics graphics = Graphics.FromImage(rotatedImage))
{
// 旋转角度为90度
graphics.RotateTransform(90);
graphics.DrawImage(originalImage, new Point(0, 0));
}
// 更新PictureBox1显示新的旋转后的图片
pictureBox1.Image = rotatedImage;
}
}
```
以上代码会在每次点击按钮时,将PictureBox1当前的图片顺时针旋转90度。
阅读全文
相关推荐















