阅读量:2
在C#中,要旋转Image对象,可以使用RotateFlip方法。以下是一个示例,展示了如何在PictureBox控件中旋转图像:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace RotateImageExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnRotate_Click(object sender, EventArgs e)
{
// 加载图像
Bitmap originalImage = new Bitmap("path/to/your/image.jpg");
// 旋转图像
Bitmap rotatedImage = RotateImage(originalImage, RotationAngle.Clockwise90);
// 将旋转后的图像显示在PictureBox中
pictureBox1.Image = rotatedImage;
}
private Bitmap RotateImage(Bitmap src, RotationAngle rotationAngle)
{
int width = src.Width;
int height = src.Height;
Bitmap rotatedBitmap = new Bitmap(height, width);
using (Graphics graphics = Graphics.FromImage(rotatedBitmap))
{
// 设置旋转角度
graphics.RotateTransform((float)rotationAngle);
// 设置图像的绘制位置
PointF destinationPoint = new PointF(0, 0);
// 绘制原始图像
graphics.DrawImage(src, destinationPoint);
}
return rotatedBitmap;
}
}
}
在这个示例中,我们创建了一个名为RotateImage的方法,该方法接受一个Bitmap对象和一个RotationAngle枚举值作为参数。RotationAngle枚举有以下三个值:
None:不旋转图像。Clockwise90:顺时针旋转90度。Counterclockwise90:逆时针旋转90度。Rotate180:旋转180度。Rotate270:旋转270度。
在btnRotate_Click方法中,我们加载了一个图像,然后调用RotateImage方法将其旋转,并将旋转后的图像显示在PictureBox控件中。
以上就是关于“c# drawimage图像如何旋转”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm