c#获取图像旋转任意角度并保存文件

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using Newtonsoft.Json;
  7 using System.Drawing;
  8 using System.IO;
  9 using System.Web;
 10 using System.Drawing.Imaging;
 11 
 12 namespace Test1
 13 {
 14     class Program
 15     {
 16         private static Bitmap _origImg;
 17 
 18         static void Main(string[] args)
 19         {
 20             ImagePath(@"C:\Users\98468\Desktop\图片识别\55.png");
 21         }
 22 
 23         public static void ImagePath(string ImgPath)
 24         {
 25             try
 26             {
 27                 //获取到图像副本
 28                 _origImg = (Bitmap)Image.FromFile(ImgPath).Clone();
 29 
 30                 string name = Path.GetFileNameWithoutExtension(ImgPath);
 31                 //获取到有扩展名得图像文件名
 32                 string name_path = Path.GetFileName(ImgPath);
 33                 //获取到图片得扩展名后缀
 34                 string path=  Path.GetExtension(ImgPath);
 35                 ImageRotate(1, name,path);
 36             }
 37             catch { }
 38 
 39         }
 40 
 41         public static void ImageRotate(int SelectedIndex,string name,string path)
 42         {
 43             try
 44             {
 45                 Bitmap bmp;
 46                 BitmapFlip(_origImg, SelectedIndex, out bmp);
 47                 //翻转后得结果
 48                 Image image = bmp;
 49                 
 50                 var A = ImageFormat.Png;
 51                 SaveToFile1(image, name + path, true, A);
 52                 string Text = "当前序号:" + SelectedIndex.ToString();
 53                 
 54             }
 55             catch { }
 56         }
 57 
 58 
 59         /// <summary>
 60         /// 图像翻转,基于图像中心
 61         /// </summary>
 62         private static void BitmapFlip(Bitmap btSource, int iFlip, out Bitmap btDes)
 63         {
 64             btDes = (Bitmap)_origImg.Clone();
 65             switch (iFlip)
 66             {
 67                 case 0:
 68                     btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); //指定后接水平翻转和垂直翻转的180度顺时针旋转。(不进行顺时针旋转和翻转)
 69                     break;
 70                 case 1:
 71                     btDes.RotateFlip(RotateFlipType.Rotate270FlipXY);  //指定不进行翻转的 90 度旋转
 72                     break;
 73                 case 2:
 74                     btDes.RotateFlip(RotateFlipType.Rotate180FlipNone);  //指定不进行翻转的 180 度旋转  (垂直翻转+水平翻转)
 75                     break;
 76                 case 3:
 77                     btDes.RotateFlip(RotateFlipType.Rotate270FlipNone);  //指定不进行翻转的 270 度旋转
 78                     break;
 79                 case 4:
 80                     btDes.RotateFlip(RotateFlipType.Rotate180FlipY);  //指定水平翻转不旋转  (水平翻转)
 81                     break;
 82                 case 5:
 83                     btDes.RotateFlip(RotateFlipType.Rotate90FlipX);  //指定90 度旋转后接水平翻转
 84                     break;
 85                 case 6:
 86                     btDes.RotateFlip(RotateFlipType.RotateNoneFlipY);  //指定180 度旋转后接水平翻转  (垂直翻转)
 87                     break;
 88                 case 7:
 89                     btDes.RotateFlip(RotateFlipType.Rotate90FlipY);  //指定270 度旋转后接水平翻转
 90                     break;
 91             }
 92 
 93 
 94 
 95 
 96         }
 97 
 98         //=======================================================
 99         //图像的保存
100         //=======================================================
101 
102         /// <summary>
103         /// 保存图像pic到默认目录中,保存名称为name
104         /// </summary>
105         public static void SaveToFile(Image pic, String name, ImageFormat format)
106         {
107             string CurDir = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.Date.ToString("yyyy_MM_dd") + @"(PngSplit)导出\";         //设置当前目录
108             if (!Directory.Exists(CurDir)) Directory.CreateDirectory(CurDir);   //该路径不存在时,在当前文件目录下创建文件夹"导出.."
109 
110             SaveToFile1(pic, CurDir + name, true, format);    //已替换方式保存图像
111         }
112        
113         //保存图像pic到文件fileName中,指定图像保存格式
114         public static void SaveToFile1(Image pic, string fileName, bool replace, ImageFormat format)    //ImageFormat.Jpeg
115         {
116             //若图像已存在,则删除
117             if(File.Exists(fileName) && replace)
118             {
119                 File.Delete(fileName);
120             }
121                 
122 
123             //若不存在则创建
124             if (!File.Exists(fileName))
125             {
126                 if (format == null)
127                 {
128                     pic.Save(fileName);
129                 }
130                 else
131                 {
132                     pic.Save(fileName);//按给定格式保存图像
133                 }    
134             }
135         }
136 
137         
138     }
139 }

 

posted @ 2022-07-12 18:29  枫亦  阅读(137)  评论(0)    收藏  举报