c#图片360度旋转

2022/1/8 17:03:57

本文主要是介绍c#图片360度旋转,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 1 public Bitmap Rotate(Bitmap b, int angle)
 2 {
 3 angle = angle % 360;
 4 
 5 //弧度转换
 6 double radian = angle * Math.PI / 180.0;
 7 double cos = Math.Cos(radian);
 8 double sin = Math.Sin(radian);
 9 
10 //原图的宽和高
11 int w = b.Width;
12 int h = b.Height;
13 int W = w;
14 int H = h;
15 //目标位图
16 Bitmap dsImage = new Bitmap(W, H);
17 Graphics g = Graphics.FromImage(dsImage);
18 
19 g.InterpolationMode = InterpolationMode.Bilinear;
20 
21 g.SmoothingMode = SmoothingMode.HighQuality;
22 
23 //计算偏移量
24 Point Offset = new Point((W - w) / 2, (H - h) / 2);
25 
26 ////构造图像显示区域:让图像的中心与窗口的中心点一致
27 Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
28 Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
29 
30 g.TranslateTransform(center.X, center.Y);
31 
32 //逆时针旋转
33 // g.RotateTransform(360 - angle);
34 
35 //顺时针旋转
36 g.RotateTransform(angle);
37 //恢复图像在水平和垂直方向的平移
38 g.TranslateTransform(-center.X, -center.Y);
39 g.Clear(Color.White);
40 
41 g.DrawImage(b, rect);
42 
43 //重至绘图的所有变换
44 g.ResetTransform();
45 
46 g.Save();
47 g.Dispose();
48 return dsImage;
49 }

代码调用,生成360度图片并保存

 1      for (int i = 0; i <= 360; i++)
 2                 {
 3                     string path = @"C:\Users\yc\Desktop\生成后图片\"+fileName;
 4                     DirectoryInfo directoryInfo = new DirectoryInfo(path);
 5                     if (!directoryInfo.Exists)
 6                     {
 7                         directoryInfo.Create();
 8                     }
 9                     Bitmap image = new Bitmap(FindResult[k].ToString());
10 
11                     Bitmap imagenew = Rotate(image, i);
12                     Image images = (Image)imagenew;
13                     string name = path+"\\" + i + ".png";
14                     images.Save(name);
15                 }

 



这篇关于c#图片360度旋转的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程