forked from itdos/Dos.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageHelper.cs
More file actions
337 lines (319 loc) · 12.6 KB
/
Copy pathImageHelper.cs
File metadata and controls
337 lines (319 loc) · 12.6 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#region << 版 本 注 释 >>
/****************************************************
* 文 件 名:EncryptHelper
* Copyright(c) www.ITdos.com
* CLR 版本: 4.0.30319.17929
* 创 建 人:ITdos
* 电子邮箱:admin@itdos.com
* 创建日期:2010/04/01 11:00:49
* 文件描述:
******************************************************
* 修 改 人:
* 修改日期:
* 备注描述:
*******************************************************/
#endregion
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace Dos.Common
{
/// <summary>
/// 图片水印处理类
/// </summary>
public class ImageHelper
{
/// <summary>
/// 生成缩略图的模式, WH-指定宽高缩放(可能变形) W-指定宽,高按比例 H-指定高,宽按比例 CUT-指定高宽裁减(不变形,推荐用这个)。
/// </summary>
public enum ThumbnailModeOption : byte
{
/// <summary>
/// 指定宽高缩放(可能变形)
/// </summary>
WH,
/// <summary>
/// 指定宽,高按比例
/// </summary>
W,
/// <summary>
/// 指定高,宽按比例
/// </summary>
H,
/// <summary>
/// 指定高宽裁减(不变形,推荐用这个)
/// </summary>
CUT
}
/// <summary>
/// 加图片水印的位置,TopLeft-左上角 TopCenter-上中间 TopRight-右上角 BottomLeft-左下角 BottomCenter-下中间 右下角-右下角 Middle-正中间。
/// </summary>
public enum WaterPositionOption : byte
{
/// <summary>
/// 左上角
/// </summary>
LeftTop,
/// <summary>
/// 上中间
/// </summary>
CenterTop,
/// <summary>
/// 右上角
/// </summary>
RightTop,
/// <summary>
/// 左下角
/// </summary>
LeftBottom,
/// <summary>
/// 下中间
/// </summary>
CenterBottom,
/// <summary>
/// 右下角
/// </summary>
RightBottom,
/// <summary>
/// 正中间
/// </summary>
Middle
}
/// <summary>
/// 获取图片格式。
/// </summary>
/// <Param name="fileName">文件名</Param>
/// <returns></returns>
public static ImageFormat GetImageFormat(string fileName)
{
string extension = fileName.Substring(fileName.LastIndexOf(".")).Trim().ToLower();
switch (extension)
{
case ".jpg":
return System.Drawing.Imaging.ImageFormat.Jpeg;
case ".jpeg":
return System.Drawing.Imaging.ImageFormat.Jpeg;
case ".gif":
return System.Drawing.Imaging.ImageFormat.Gif;
case ".png":
return System.Drawing.Imaging.ImageFormat.Png;
case ".bmp":
return System.Drawing.Imaging.ImageFormat.Bmp;
case ".ico":
return System.Drawing.Imaging.ImageFormat.Icon;
default:
goto case ".jpg";
}
}
/// <summary>
/// 加水印图片并保存。
/// </summary>
/// <Param name="originalImageStream">Stream</Param>
/// <Param name="strFileName">源图路径(物理路径)</Param>
/// <Param name="savePath">图片保存路径(物理路径)</Param>
/// <Param name="waterPath">水印图路径(物理路径)</Param>
/// <Param name="edge">水印图离原图边界的距离</Param>
/// <Param name="position">加图片水印的位置</Param>
/// <returns>是否成功</returns>
public static bool MakeWaterImage(Stream originalImageStream, string strFileName, string savePath, string waterPath, int edge, WaterPositionOption position)
{
bool success = false;
int x = 0;
int y = 0;
Image waterImage = null;
Image image = null;
Bitmap bitmap = null;
Graphics graphics = null;
try
{
//加载原图
image = Image.FromStream(originalImageStream);
//加载水印图
waterImage = Image.FromFile(waterPath);
bitmap = new Bitmap(image);
graphics = Graphics.FromImage(bitmap);
int newEdge = edge;
if (newEdge >= image.Width + waterImage.Width) newEdge = 10;
switch (position)
{
case WaterPositionOption.LeftTop:
x = newEdge;
y = newEdge;
break;
case WaterPositionOption.CenterTop:
x = (image.Width - waterImage.Width) / 2;
y = newEdge;
break;
case WaterPositionOption.RightTop:
x = image.Width - waterImage.Width - newEdge;
y = newEdge;
break;
case WaterPositionOption.LeftBottom:
x = newEdge;
y = image.Height - waterImage.Height - newEdge;
break;
case WaterPositionOption.CenterBottom:
x = (image.Width - waterImage.Width) / 2;
y = image.Height - waterImage.Height - newEdge;
break;
case WaterPositionOption.RightBottom:
x = image.Width - waterImage.Width - newEdge;
y = image.Height - waterImage.Height - newEdge;
break;
case WaterPositionOption.Middle:
x = (image.Width - waterImage.Width) / 2;
y = (image.Height - waterImage.Height) / 2;
break;
default:
goto case WaterPositionOption.RightBottom;
}
// 画水印图片
graphics.DrawImage(waterImage, new Rectangle(x, y, waterImage.Width, waterImage.Height), 0, 0, waterImage.Width, waterImage.Height, GraphicsUnit.Pixel);
// 关闭打开着的文件并保存(覆盖)新图片
originalImageStream.Close();
bitmap.Save(savePath, GetImageFormat(strFileName));
success = true;
}
catch (Exception ex)
{
success = false;
throw;
//throw new Exception(ex.Message.Replace("'", " ").Replace("\n", " ").Replace("\\", "/"));
}
finally
{
if (graphics != null) graphics.Dispose();
if (bitmap != null) bitmap.Dispose();
if (image != null) image.Dispose();
if (waterImage != null) waterImage.Dispose();
}
return success;
}
/// <summary>
/// 生成缩略图并保存。
/// </summary>
/// <Param name="originalImageStream">Stream</Param>
/// <Param name="strFileName">源图路径(物理路径)</Param>
/// <Param name="thumbnailPath">缩略图路径(物理路径)</Param>
/// <Param name="maxWidth">缩略图最大宽度</Param>
/// <Param name="maxheight">缩略图最大高度</Param>
/// <Param name="mode">生成缩略图的方式</Param>
/// <returns>是否成功</returns>
public static bool MakeThumbnail(Stream originalImageStream, string strFileName, string thumbnailPath, int maxWidth, int maxheight, ThumbnailModeOption mode)
{
bool success = false;
int x = 0;
int y = 0;
int toW = maxWidth;
int toH = maxheight;
Image image = null;
Image bitmap = null;
Graphics graphics = null;
try
{
image = Image.FromStream(originalImageStream);
int w = image.Width;
int h = image.Height;
switch (mode)
{
case ThumbnailModeOption.WH:
break;
case ThumbnailModeOption.W:
if (w < maxWidth)
{
toW = w;
toH = h;
}
else
{
toH = h * maxWidth / w;
}
break;
case ThumbnailModeOption.H:
if (h < maxheight)
{
toW = w;
toH = h;
}
else
{
toW = w * maxheight / h;
}
break;
case ThumbnailModeOption.CUT:
if (((double)w / (double)h) > ((double)toW / (double)toH))
{
w = h * toW / toH;
y = 0;
x = (image.Width - w) / 2;
}
else
{
h = w * toH / toW;
x = 0;
y = (image.Height - h) / 2;
}
break;
default:
goto case ThumbnailModeOption.CUT;
}
bitmap = new Bitmap(toW, toH);
graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度
graphics.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充
graphics.Clear(Color.Transparent);
// 在指定位置并且按指定大小绘制原图片的指定部分
graphics.DrawImage(image, new Rectangle(0, 0, toW, toH), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
// 保存缩略图
bitmap.Save(thumbnailPath, GetImageFormat(strFileName));
success = true;
}
catch (Exception ex)
{
success = false;
throw;
}
finally
{
if (graphics != null) graphics.Dispose();
if (bitmap != null) bitmap.Dispose();
if (image != null) image.Dispose();
}
return success;
}
/// <summary>
/// 生成缩略图并打水印再保存。
/// </summary>
/// <Param name="originalImageStream">Stream</Param>
/// <Param name="strFileName">源图路径(物理路径)</Param>
/// <Param name="thumbnailPath">缩略图路径(物理路径)</Param>
/// <Param name="maxWidth">缩略图最大宽度</Param>
/// <Param name="maxheight">缩略图最大高度</Param>
/// <Param name="mode">生成缩略图的方式</Param>
/// <Param name="waterPath">水印图路径(物理路径)</Param>
/// <Param name="edge">水印图离原图边界的距离</Param>
/// <Param name="position">加图片水印的位置</Param>
/// <returns>是否成功</returns>
public static bool MakeThumbnailWater(Stream originalImageStream, string strFileName, string thumbnailPath, int maxWidth, int maxheight, ThumbnailModeOption mode, string waterPath, int edge, WaterPositionOption position)
{
bool success = false;
try
{
// 生成缩略图
MakeThumbnail(originalImageStream, strFileName, thumbnailPath, maxWidth, maxheight, mode);
Stream stream = File.Open(thumbnailPath, FileMode.Open);
//打水印
MakeWaterImage(stream, strFileName, thumbnailPath, waterPath, edge, position);
success = true;
}
catch (Exception ex)
{
success = false;
throw;
}
return success;
}
}
}