0%

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

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Drawing;
using System.IO;
using System.Web;
using System.Drawing.Imaging;

namespace Test1
{
class Program
{
private static Bitmap _origImg;

static void Main(string[] args)
{
ImagePath(@"C:\Users\98468\Desktop\图片识别\55.png");
}

public static void ImagePath(string ImgPath)
{
try
{
//获取到图像副本
_origImg = (Bitmap)Image.FromFile(ImgPath).Clone();

string name = Path.GetFileNameWithoutExtension(ImgPath);
//获取到有扩展名得图像文件名
string name_path = Path.GetFileName(ImgPath);
//获取到图片得扩展名后缀
string path= Path.GetExtension(ImgPath);
ImageRotate(1, name,path);
}
catch { }

}

public static void ImageRotate(int SelectedIndex,string name,string path)
{
try
{
Bitmap bmp;
BitmapFlip(_origImg, SelectedIndex, out bmp);
//翻转后得结果
Image image = bmp;

var A = ImageFormat.Png;
SaveToFile1(image, name + path, true, A);
string Text = "当前序号:" + SelectedIndex.ToString();

}
catch { }
}


/// <summary>
/// 图像翻转,基于图像中心
/// </summary>
private static void BitmapFlip(Bitmap btSource, int iFlip, out Bitmap btDes)
{
btDes = (Bitmap)_origImg.Clone();
switch (iFlip)
{
case 0:
btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); //指定后接水平翻转和垂直翻转的180度顺时针旋转。(不进行顺时针旋转和翻转)
break;
case 1:
btDes.RotateFlip(RotateFlipType.Rotate270FlipXY); //指定不进行翻转的 90 度旋转
break;
case 2:
btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); //指定不进行翻转的 180 度旋转 (垂直翻转+水平翻转)
break;
case 3:
btDes.RotateFlip(RotateFlipType.Rotate270FlipNone); //指定不进行翻转的 270 度旋转
break;
case 4:
btDes.RotateFlip(RotateFlipType.Rotate180FlipY); //指定水平翻转不旋转 (水平翻转)
break;
case 5:
btDes.RotateFlip(RotateFlipType.Rotate90FlipX); //指定90 度旋转后接水平翻转
break;
case 6:
btDes.RotateFlip(RotateFlipType.RotateNoneFlipY); //指定180 度旋转后接水平翻转 (垂直翻转)
break;
case 7:
btDes.RotateFlip(RotateFlipType.Rotate90FlipY); //指定270 度旋转后接水平翻转
break;
}




}

//=======================================================
//图像的保存
//=======================================================

/// <summary>
/// 保存图像pic到默认目录中,保存名称为name
/// </summary>
public static void SaveToFile(Image pic, String name, ImageFormat format)
{
string CurDir = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.Date.ToString("yyyy_MM_dd") + @"(PngSplit)导出\"; //设置当前目录
if (!Directory.Exists(CurDir)) Directory.CreateDirectory(CurDir); //该路径不存在时,在当前文件目录下创建文件夹"导出.."

SaveToFile1(pic, CurDir + name, true, format); //已替换方式保存图像
}

//保存图像pic到文件fileName中,指定图像保存格式
public static void SaveToFile1(Image pic, string fileName, bool replace, ImageFormat format) //ImageFormat.Jpeg
{
//若图像已存在,则删除
if(File.Exists(fileName) && replace)
{
File.Delete(fileName);
}


//若不存在则创建
if (!File.Exists(fileName))
{
if (format == null)
{
pic.Save(fileName);
}
else
{
pic.Save(fileName);//按给定格式保存图像
}
}
}


}
}