首页 > 其他分享 >根据图片获得360°旋转图片

根据图片获得360°旋转图片

时间:2022-12-06 14:55:44浏览次数:58  
标签:img int Image 旋转 360 file new Math 图片

 1  public class ImageHelper
 2     {
 3         /// <summary> 
 4         /// 以逆时针为方向对图像进行旋转 
 5         /// </summary> 
 6         /// <param name="b">位图流</param> 
 7         /// <param name="angle">旋转角度[0,360](前台给的)</param> 
 8         /// <returns></returns> 
 9         public Image RotateImg(Image b, int angle, string file)
10         {
11             angle = angle % 360;
12             //弧度转换 
13             double radian = angle * Math.PI / 180.0;
14             double cos = Math.Cos(radian);
15             double sin = Math.Sin(radian);
16             //原图的宽和高 
17             int w = b.Width;
18             int h = b.Height;
19             int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
20             int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
21             //目标位图 
22             Bitmap dsImage = new Bitmap(W, H);
23             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
24             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
25             //背景颜色,无则透明
26             g.Clear(System.Drawing.Color.White);
27             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
28             //计算偏移量 
29             Point Offset = new Point((W - w) / 2, (H - h) / 2);
30             //构造图像显示区域:让图像的中心与窗口的中心点一致 
31             Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
32             Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
33             g.TranslateTransform(center.X, center.Y);
34             g.RotateTransform(angle);
35             //恢复图像在水平和垂直方向的平移 
36             g.TranslateTransform(-center.X, -center.Y);
37             g.DrawImage(b, rect);
38             //重至绘图的所有变换 
39             g.ResetTransform();
40             g.Save();
41             g.Dispose();
42 
43             int aasda = (dsImage.Width - 350) / 2;
44             Console.WriteLine(aasda);
45             Image image = CropImage(dsImage, rect);
46             //保存旋转后的图片 
47             image.Save(@"D:\img\" + Path.GetFileNameWithoutExtension(file) + "\\" + angle + ".png", System.Drawing.Imaging.ImageFormat.Png);
48             return dsImage;
49         }
50 
51         /// <summary>
52         /// 裁剪图片
53         /// </summary>
54         /// <param name="originImage">原图片</param>
55         /// <param name="region">裁剪的方形区域</param>
56         /// <returns>裁剪后图片</returns>
57         public Image CropImage(Image originImage, Rectangle region)
58         {
59             Bitmap result = new Bitmap(region.Width, region.Height);
60             Graphics graphics = Graphics.FromImage(result);
61             graphics.DrawImage(originImage, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel);
62             return result;
63         }
64 
65 
66         public Image RotateImg(string filename, int angle, string file)
67         {
68             return RotateImg(GetSourceImg(filename), angle, file);
69         }
70         public Image GetSourceImg(string filename)
71         {
72             Image img;
73             img = Bitmap.FromFile(filename);
74             return img;
75         }
76     }

调用一下

 1 void mian(){
 2     string[] strArr = Directory.GetFiles(@"D:\img");
 3             foreach (string file in strArr)
 4             {
 5                 Console.WriteLine(file);   //输出E:\123\新建文本文件.txt
 6                 Console.WriteLine(Path.GetFileNameWithoutExtension(file));
 7                 //如果要保存的目录不存在,则先创建
 8                 if (!Directory.Exists(@"D:\img\" + Path.GetFileNameWithoutExtension(file)))
 9                 {
10                     Directory.CreateDirectory(@"D:\img\" + Path.GetFileNameWithoutExtension(file));
11                 }
12                 FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
13                 Image img = Bitmap.FromStream(fs);
14                 ImageHelper IH = new ImageHelper();
15                 for (int i = 1; i <= 360; i++)
16                 {
17                     IH.RotateImg(img, i, file);
18                 }
19                 fs.Close();
20             }
21 }

 

利用C#代码实现图片旋转360度_C#教程_脚本之家 (jb51.net) 做了一定的更改

标签:img,int,Image,旋转,360,file,new,Math,图片
From: https://www.cnblogs.com/galatia/p/16955216.html

相关文章