1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 using System.Drawing.Drawing2D;
7
8 namespace HelloCsharp.Utilities
9 {
10 public class ImageClass
11 {
12 public ImageClass()
13 { }
14
15 #region 缩略图
16 ///
17
18 /// 生成缩略图
19 ///
20
21 ///源图路径(物理路径) ///缩略图路径(物理路径) ///缩略图宽度 ///缩略图高度 ///生成缩略图的方式 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
22 {
23 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
24
25 int towidth = width;
26 int toheight = height;
27
28 int x = 0;
29 int y = 0;
30 int ow = originalImage.Width;
31 int oh = originalImage.Height;
32
33 switch (mode)
34 {
35 case "HW": //指定高宽缩放(可能变形)
36 break;
37 case "W": //指定宽,高按比例
38 toheight = originalImage.Height * width / originalImage.Width;
39 break;
40 case "H": //指定高,宽按比例
41 towidth = originalImage.Width * height / originalImage.Height;
42 break;
43 case "Cut": //指定高宽裁减(不变形)
44 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
45 {
46 oh = originalImage.Height;
47 ow = originalImage.Height * towidth / toheight;
48 y = 0;
49 x = (originalImage.Width - ow) / 2;
50 }
51 else
52 {
53 ow = originalImage.Width;
54 oh = originalImage.Width * height / towidth;
55 x = 0;
56 y = (originalImage.Height - oh) / 2;
57 }
58 break;
59 default:
60 break;
61 }
62
63 //新建一个bmp图片
64 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
65
66 //新建一个画板
67 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
68
69 //设置高质量插值法
70 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
71
72 //设置高质量,低速度呈现平滑程度
73 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
74
75 //清空画布并以透明背景色填充
76 g.Clear(System.Drawing.Color.Transparent);
77
78 //在指定位置并且按指定大小绘制原图片的指定部分
79 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
80
81 try
82 {
83 //以jpg格式保存缩略图
84 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
85 }
86 catch (System.Exception e)
87 {
88 throw e;
89 }
90 finally
91 {
92 originalImage.Dispose();
93 bitmap.Dispose();
94 g.Dispose();
95 }
96 }
97 #endregion
98
99 #region 图片水印
100 ///
101
102 /// 图片水印处理方法
103 ///
104
105 ///需要加载水印的图片路径(绝对路径) ///水印图片(绝对路径) ///水印位置(传送正确的代码) public static string ImageWatermark(string path, string waterpath, string location)
106 {
107 string kz_name = Path.GetExtension(path);
108 if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
109 {
110 DateTime time = DateTime.Now;
111 string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
112 Image img = Bitmap.FromFile(path);
113 Image waterimg = Image.FromFile(waterpath);
114 Graphics g = Graphics.FromImage(img);
115 ArrayList loca = GetLocation(location, img, waterimg);
116 g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
117 waterimg.Dispose();
118 g.Dispose();
119 string newpath = Path.GetDirectoryName(path) + filename + kz_name;
120 img.Save(newpath);
121 img.Dispose();
122 File.Copy(newpath, path, true);
123 if (File.Exists(newpath))
124 {
125 File.Delete(newpath);
126 }
127 }
128 return path;
129 }
130
131 ///
132
133 /// 图片水印位置处理方法
134 ///
135
136 ///水印位置 ///需要添加水印的图片 ///水印图片 private static ArrayList GetLocation(string location, Image img, Image waterimg)
137 {
138 ArrayList loca = new ArrayList();
139 int x = 0;
140 int y = 0;
141
142 if (location == "LT")
143 {
144 x = 10;
145 y = 10;
146 }
147 else if (location == "T")
148 {
149 x = img.Width / 2 - waterimg.Width / 2;
150 y = img.Height - waterimg.Height;
151 }
152 else if (location == "RT")
153 {
154 x = img.Width - waterimg.Width;
155 y = 10;
156 }
157 else if (location == "LC")
158 {
159 x = 10;
160 y = img.Height / 2 - waterimg.Height / 2;
161 }
162 else if (location == "C")
163 {
164 x = img.Width / 2 - waterimg.Width / 2;
165 y = img.Height / 2 - waterimg.Height / 2;
166 }
167 else if (location == "RC")
168 {
169 x = img.Width - waterimg.Width;
170 y = img.Height / 2 - waterimg.Height / 2;
171 }
172 else if (location == "LB")
173 {
174 x = 10;
175 y = img.Height - waterimg.Height;
176 }
177 else if (location == "B")
178 {
179 x = img.Width / 2 - waterimg.Width / 2;
180 y = img.Height - waterimg.Height;
181 }
182 else
183 {
184 x = img.Width - waterimg.Width;
185 y = img.Height - waterimg.Height;
186 }
187 loca.Add(x);
188 loca.Add(y);
189 return loca;
190 }
191 #endregion
192
193 #region 文字水印
194 ///
195
196 /// 文字水印处理方法
197 ///
198
199 ///图片路径(绝对路径) ///字体大小 ///水印文字 ///颜色 ///水印位置 public static string LetterWatermark(string path, int size, string letter, Color color, string location)
200 {
201 #region
202
203 string kz_name = Path.GetExtension(path);
204 if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
205 {
206 DateTime time = DateTime.Now;
207 string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
208 Image img = Bitmap.FromFile(path);
209 Graphics gs = Graphics.FromImage(img);
210 ArrayList loca = GetLocation(location, img, size, letter.Length);
211 Font font = new Font("宋体", size);
212 Brush br = new SolidBrush(color);
213 gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString()));
214 gs.Dispose();
215 string newpath = Path.GetDirectoryName(path) + filename + kz_name;
216 img.Save(newpath);
217 img.Dispose();
218 File.Copy(newpath, path, true);
219 if (File.Exists(newpath))
220 {
221 File.Delete(newpath);
222 }
223 }
224 return path;
225
226 #endregion
227 }
228
229 ///
230
231 /// 文字水印位置的方法
232 ///
233
234 ///位置代码 ///图片对象 ///宽(当水印类型为文字时,传过来的就是字体的大小) ///高(当水印类型为文字时,传过来的就是字符的长度) private static ArrayList GetLocation(string location, Image img, int width, int height)
235 {
236 #region
237
238 ArrayList loca = new ArrayList(); //定义数组存储位置
239 float x = 10;
240 float y = 10;
241
242 if (location == "LT")
243 {
244 loca.Add(x);
245 loca.Add(y);
246 }
247 else if (location == "T")
248 {
249 x = img.Width / 2 - (width * height) / 2;
250 loca.Add(x);
251 loca.Add(y);
252 }
253 else if (location == "RT")
254 {
255 x = img.Width - width * height;
256 }
257 else if (location == "LC")
258 {
259 y = img.Height / 2;
260 }
261 else if (location == "C")
262 {
263 x = img.Width / 2 - (width * height) / 2;
264 y = img.Height / 2;
265 }
266 else if (location == "RC")
267 {
268 x = img.Width - height;
269 y = img.Height / 2;
270 }
271 else if (location == "LB")
272 {
273 y = img.Height - width - 5;
274 }
275 else if (location == "B")
276 {
277 x = img.Width / 2 - (width * height) / 2;
278 y = img.Height - width - 5;
279 }
280 else
281 {
282 x = img.Width - width * height;
283 y = img.Height - width - 5;
284 }
285 loca.Add(x);
286 loca.Add(y);
287 return loca;
288
289 #endregion
290 }
291 #endregion
292
293 #region 调整光暗
294 ///
295
296 /// 调整光暗
297 ///
298
299 ///原始图片 ///原始图片的长度 ///原始图片的高度 ///增加或减少的光暗值 public Bitmap LDPic(Bitmap mybm, int width, int height, int val)
300 {
301 Bitmap bm = new Bitmap(width, height);//初始化一个记录经过处理后的图片对象
302 int x, y, resultR, resultG, resultB;//x、y是循环次数,后面三个是记录红绿蓝三个值的
303 Color pixel;
304 for (x = 0; x < width; x++)
305 {
306 for (y = 0; y < height; y++)
307 {
308 pixel = mybm.GetPixel(x, y);//获取当前像素的值
309 resultR = pixel.R + val;//检查红色值会不会超出[0, 255]
310 resultG = pixel.G + val;//检查绿色值会不会超出[0, 255]
311 resultB = pixel.B + val;//检查蓝色值会不会超出[0, 255]
312 bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
313 }
314 }
315 return bm;
316 }
317 #endregion
318
319 #region 反色处理
320 ///
321
322 /// 反色处理
323 ///
324
325 ///原始图片 ///原始图片的长度 ///原始图片的高度 public Bitmap RePic(Bitmap mybm, int width, int height)
326 {
327 Bitmap bm = new Bitmap(width, height);//初始化一个记录处理后的图片的对象
328 int x, y, resultR, resultG, resultB;
329 Color pixel;
330 for (x = 0; x < width; x++)
331 {
332 for (y = 0; y < height; y++)
333 {
334 pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
335 resultR = 255 - pixel.R;//反红
336 resultG = 255 - pixel.G;//反绿
337 resultB = 255 - pixel.B;//反蓝
338 bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
339 }
340 }
341 return bm;
342 }
343 #endregion
344
345 #region 浮雕处理
346 ///
347
348 /// 浮雕处理
349 ///
350
351 ///原始图片 ///原始图片的长度 ///原始图片的高度 public Bitmap FD(Bitmap oldBitmap, int Width, int Height)
352 {
353 Bitmap newBitmap = new Bitmap(Width, Height);
354 Color color1, color2;
355 for (int x = 0; x < Width - 1; x++)
356 {
357 for (int y = 0; y < Height - 1; y++) { int r = 0, g = 0, b = 0; color1 = oldBitmap.GetPixel(x, y); color2 = oldBitmap.GetPixel(x + 1, y + 1); r = Math.Abs(color1.R - color2.R + 128); g = Math.Abs(color1.G - color2.G + 128); b = Math.Abs(color1.B - color2.B + 128); if (r > 255) r = 255;
358 if (r < 0) r = 0; if (g > 255) g = 255;
359 if (g < 0) g = 0; if (b > 255) b = 255;
360 if (b < 0) b = 0;
361 newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
362 }
363 }
364 return newBitmap;
365 }
366 #endregion
367
368 #region 拉伸图片
369 ///
370
371 /// 拉伸图片
372 ///
373
374 ///原始图片 ///新的宽度 ///新的高度 public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
375 {
376 try
377 {
378 Bitmap bap = new Bitmap(newW, newH);
379 Graphics g = Graphics.FromImage(bap);
380 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
381 g.DrawImage(bap, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bap.Width, bap.Height), GraphicsUnit.Pixel);
382 g.Dispose();
383 return bap;
384 }
385 catch
386 {
387 return null;
388 }
389 }
390 #endregion
391
392 #region 滤色处理
393 ///
394
395 /// 滤色处理
396 ///
397
398 ///原始图片 ///原始图片的长度 ///原始图片的高度 public Bitmap FilPic(Bitmap mybm, int width, int height)
399 {
400 Bitmap bm = new Bitmap(width, height);//初始化一个记录滤色效果的图片对象
401 int x, y;
402 Color pixel;
403
404 for (x = 0; x < width; x++)
405 {
406 for (y = 0; y < height; y++)
407 {
408 pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
409 bm.SetPixel(x, y, Color.FromArgb(0, pixel.G, pixel.B));//绘图
410 }
411 }
412 return bm;
413 }
414 #endregion
415
416 #region 左右翻转
417 ///
418
419 /// 左右翻转
420 ///
421
422 ///原始图片 ///原始图片的长度 ///原始图片的高度 public Bitmap RevPicLR(Bitmap mybm, int width, int height)
423 {
424 Bitmap bm = new Bitmap(width, height);
425 int x, y, z; //x,y是循环次数,z是用来记录像素点的x坐标的变化的
426 Color pixel;
427 for (y = height - 1; y >= 0; y--)
428 {
429 for (x = width - 1, z = 0; x >= 0; x--)
430 {
431 pixel = mybm.GetPixel(x, y);//获取当前像素的值
432 bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
433 }
434 }
435 return bm;
436 }
437 #endregion
438
439 #region 上下翻转
440 ///
441
442 /// 上下翻转
443 ///
444
445 ///原始图片 ///原始图片的长度 ///原始图片的高度 public Bitmap RevPicUD(Bitmap mybm, int width, int height)
446 {
447 Bitmap bm = new Bitmap(width, height);
448 int x, y, z;
449 Color pixel;
450 for (x = 0; x < width; x++) { for (y = height - 1, z = 0; y >= 0; y--)
451 {
452 pixel = mybm.GetPixel(x, y);//获取当前像素的值
453 bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
454 }
455 }
456 return bm;
457 }
458 #endregion
459
460 #region 压缩图片
461 ///
462
463 /// 压缩到指定尺寸
464 ///
465
466 ///原文件 ///新文件 public bool Compress(string oldfile, string newfile)
467 {
468 try
469 {
470 System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
471 System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
472 Size newSize = new Size(100, 125);
473 Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
474 Graphics g = Graphics.FromImage(outBmp);
475 g.CompositingQuality = CompositingQuality.HighQuality;
476 g.SmoothingMode = SmoothingMode.HighQuality;
477 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
478 g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
479 g.Dispose();
480 EncoderParameters encoderParams = new EncoderParameters();
481 long[] quality = new long[1];
482 quality[0] = 100;
483 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
484 encoderParams.Param[0] = encoderParam;
485 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
486 ImageCodecInfo jpegICI = null;
487 for (int x = 0; x < arrayICI.Length; x++)
488 if (arrayICI[x].FormatDescription.Equals("JPEG"))
489 {
490 jpegICI = arrayICI[x]; //设置JPEG编码
491 break;
492 }
493 img.Dispose();
494 if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
495 outBmp.Dispose();
496 return true;
497 }
498 catch
499 {
500 return false;
501 }
502 }
503 #endregion
504
505 #region 图片灰度化
506 public Color Gray(Color c)
507 {
508 int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
509 return Color.FromArgb(rgb, rgb, rgb);
510 }
511 #endregion
512
513 #region 转换为黑白图片
514 ///
515
516 /// 转换为黑白图片
517 ///
518
519 ///要进行处理的图片 ///图片的长度 ///图片的高度 public Bitmap BWPic(Bitmap mybm, int width, int height)
520 {
521 Bitmap bm = new Bitmap(width, height);
522 int x, y, result; //x,y是循环次数,result是记录处理后的像素值
523 Color pixel;
524 for (x = 0; x < width; x++)
525 {
526 for (y = 0; y < height; y++)
527 {
528 pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
529 result = (pixel.R + pixel.G + pixel.B) / 3;//取红绿蓝三色的平均值
530 bm.SetPixel(x, y, Color.FromArgb(result, result, result));
531 }
532 }
533 return bm;
534 }
535 #endregion
536
537 #region 获取图片中的各帧
538 ///
539
540 /// 获取图片中的各帧
541 ///
542
543 ///图片路径 ///保存路径 public void GetFrames(string pPath, string pSavedPath)
544 {
545 Image gif = Image.FromFile(pPath);
546 FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
547 int count = gif.GetFrameCount(fd); //获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
548 for (int i = 0; i < count; i++) //以Jpeg格式保存各帧
549 {
550 gif.SelectActiveFrame(fd, i);
551 gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
552 }
553 }
554 #endregion
555 }
556 }