/
ImageCls.cs
230 lines (210 loc) · 7.89 KB
/
ImageCls.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Linq;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace Hange.Utility
{
public class ImageCls:IDisposable
{
private static Image _oldImg;
private static Graphics _newGraphics;
private static string _ext = ".jpeg";
/// <summary>
/// 构造函数
/// </summary>
/// <param name="oldPath">图片路径</param>
public ImageCls(string oldPath)
{
try
{
_ext = Path.GetExtension(oldPath);
_oldImg = Image.FromFile(oldPath);
}
catch{}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="oldStream">图片流</param>
public ImageCls(Stream oldStream)
{
try
{
_oldImg = Image.FromStream(oldStream);
}
catch { }
}
/// <summary>
/// 压缩图片,返回map
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public Bitmap ResizeImg(int width, int height)
{
if (_oldImg == null)
return null;
int ow = _oldImg.Width, oh = _oldImg.Height; //原始大小
if (height == -2)
{
var rale = (double)width / Math.Max(ow, oh); //压缩比例
width = (int)Math.Ceiling(ow * rale);
height = (int)Math.Ceiling(oh * rale);
}
else
{
width = (width != -1 ? width : ow);
height = (height != -1 ? height : oh);
}
var bm = new Bitmap(width, height, PixelFormat.Format32bppRgb);
_newGraphics = Graphics.FromImage(bm);
//呈现质量
_newGraphics.CompositingQuality = CompositingQuality.HighQuality;
//像素偏移方式
_newGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//平滑处理
_newGraphics.SmoothingMode = SmoothingMode.HighQuality;
//插补模式,双三次插值法
_newGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
_newGraphics.Clear(Color.Transparent);
_newGraphics.DrawImage(_oldImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, ow, oh),
GraphicsUnit.Pixel);
//return "处理成功";
return bm;
}
/// <summary>
/// 压缩图片(简化),返回map
/// </summary>
/// <param name="width"></param>
/// <returns></returns>
public Bitmap ResizeImg(int width)
{
return ResizeImg(width, -2);
}
/// <summary>
/// 压缩图片,返回结果
/// </summary>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="newPath">新路径</param>
/// <param name="qt">压缩质量(0-100)</param>
/// <returns></returns>
public bool ResizeImg(int width, int height, string newPath, int qt)
{
try
{
var bm = ResizeImg(width, height);
var encoder = GetEncoderInfo(_ext);
if (encoder == null)
return false;
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, qt);
bm.Save(newPath, encoder, encoderParameters);
return true;
}
catch(Exception)
{
return false;
}
}
/// <summary>
/// 压缩图片(简化),返回结果
/// </summary>
/// <param name="width">宽</param>
/// <param name="newPath">新路径</param>
/// <param name="qt">压缩质量(0-100)</param>
/// <returns></returns>
public bool ResizeImg(int width,string newPath,int qt)
{
return ResizeImg(width, -2, newPath, qt);
}
/// <summary>
/// 压缩图片(简化),返回结果
/// </summary>
/// <param name="newPath">新路径</param>
/// <param name="qt">压缩质量(0-100)</param>
/// <returns></returns>
public bool ResizeImg(string newPath, int qt)
{
return ResizeImg(-1, -1, newPath, qt);
}
/// <summary>
/// 压缩图片(简化),返回结果
/// </summary>
/// <param name="width">宽</param>
/// <param name="newPath">新路径</param>
/// <returns></returns>
public bool ResizeImg(int width, string newPath)
{
return ResizeImg(width, -2, newPath, 80);
}
/// <summary>
/// 剪切图片
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
/// <param name="h"></param>
/// <param name="width">保存图片宽</param>
/// <param name="height">保存图片高</param>
/// <param name="newPath">新路径</param>
/// <param name="qt"></param>
/// <returns></returns>
public bool CutImage(int x, int y, int w, int h, int width, int height, string newPath, int qt)
{
if (_oldImg == null)
return false;
int ow = _oldImg.Width, oh = _oldImg.Height; //原始大小
if (x > ow || y > oh)
return false;
if (x + w > ow)
w = ow - x;
if (y + h > oh)
h = oh - y;
if (width <= 0)
width = w;
if (height <= 0)
height = h;
var bm = new Bitmap(width, height, PixelFormat.Format32bppRgb);
_newGraphics = Graphics.FromImage(bm);
//呈现质量
_newGraphics.CompositingQuality = CompositingQuality.HighQuality;
//像素偏移方式
_newGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//平滑处理
_newGraphics.SmoothingMode = SmoothingMode.HighQuality;
//插补模式,双三次插值法
_newGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
_newGraphics.Clear(Color.Transparent);
_newGraphics.DrawImage(_oldImg, new Rectangle(0, 0, width, height), new Rectangle(x, y, w, h),
GraphicsUnit.Pixel);
//return "处理成功";
var encoder = GetEncoderInfo(_ext);
if (encoder == null)
return false;
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, qt);
bm.Save(newPath, encoder, encoderParameters);
return true;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
//根据 mime 类型,返回编码器
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
mimeType = "image/" + mimeType.Replace(".", "").ToLower();
mimeType = mimeType.Replace("jpg", "jpeg");
return encoders.FirstOrDefault(t => t.MimeType == mimeType);
}
#region IDisposable 成员
void IDisposable.Dispose()
{
if (_oldImg != null)
_oldImg.Dispose();
if (_newGraphics != null)
_newGraphics.Dispose();
}
#endregion
}
}