利用C#代码实现图片旋转360度
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Drawing;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Text;
namespace图片旋转程序
{
publicclassImageHelper
{
///<summary>
///以逆时针为方向对图像进行旋转
///</summary>
///<paramname="b">位图流</param>
///<paramname="angle">旋转角度[0,360](前台给的)</param>
///<returns></returns>
publicImageRotateImg(Imageb,intangle,stringfile)
{
angle=angle%360;
//弧度转换
doubleradian=angle*Math.PI/180.0;
doublecos=Math.Cos(radian);
doublesin=Math.Sin(radian);
//原图的宽和高
intw=b.Width;
inth=b.Height;
intW=(int)(Math.Max(Math.Abs(w*cos-h*sin),Math.Abs(w*cos+h*sin)));
intH=(int)(Math.Max(Math.Abs(w*sin-h*cos),Math.Abs(w*sin+h*cos)));
//目标位图
BitmapdsImage=newBitmap(W,H);
System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//计算偏移量
PointOffset=newPoint((W-w)/2,(H-h)/2);
//构造图像显示区域:让图像的中心与窗口的中心点一致
Rectanglerect=newRectangle(Offset.X,Offset.Y,w,h);
Pointcenter=newPoint(rect.X+rect.Width/2,rect.Y+rect.Height/2);
g.TranslateTransform(center.X,center.Y);
g.RotateTransform(angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X,-center.Y);
g.DrawImage(b,rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
//保存旋转后的图片
dsImage.Save(@"D:\img\"+Path.GetFileNameWithoutExtension(file)+"\\"+angle+".png",System.Drawing.Imaging.ImageFormat.Png);
returndsImage;
}
publicImageRotateImg(stringfilename,intangle,stringfile)
{
returnRotateImg(GetSourceImg(filename),angle,file);
}
publicImageGetSourceImg(stringfilename)
{
Imageimg;
img=Bitmap.FromFile(filename);
returnimg;
}
}
classProgram
{
staticvoidMain(string[]args)
{
string[]strArr=Directory.GetFiles(@"D:\img");
foreach(stringfileinstrArr)
{
Console.WriteLine(file);//输出E:\123\新建文本文件.txt
Console.WriteLine(Path.GetFileNameWithoutExtension(file));
//如果要保存的目录不存在,则先创建
if(!Directory.Exists(@"D:\img\"+Path.GetFileNameWithoutExtension(file)))
{
Directory.CreateDirectory(@"D:\img\"+Path.GetFileNameWithoutExtension(file));
}
FileStreamfs=newFileStream(file,FileMode.Open,FileAccess.Read);
Imageimg=Bitmap.FromStream(fs);
ImageHelperIH=newImageHelper();
for(inti=1;i<=360;i++)
{
IH.RotateImg(img,i,file);
}
fs.Close();
}
Console.ReadKey();
}
}
}
以上所述是本文的全部内容,有问题的可以和小编联系,谢谢对毛票票的支持!