PicDeal.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Drawing.Imaging;
  3. using System.Collections;
  4. using System.Drawing;
  5. using System.IO;
  6. namespace Ant.Service.Utilities
  7. {
  8. /// <summary>
  9. /// 枚举,生成缩略图模式
  10. /// </summary>
  11. public enum ThumbnailMod : byte
  12. {
  13. /// <summary>
  14. /// HW
  15. /// </summary>
  16. HW,
  17. /// <summary>
  18. /// W
  19. /// </summary>
  20. W,
  21. /// <summary>
  22. /// H
  23. /// </summary>
  24. H,
  25. /// <summary>
  26. /// Cut
  27. /// </summary>
  28. Cut
  29. };
  30. /// <summary>
  31. /// 操作图片类, 生成缩略图,添加水印
  32. /// </summary>
  33. public static class PicDeal
  34. {
  35. private static Hashtable htmimes = new Hashtable();
  36. internal static readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
  37. #region 生成缩略图
  38. /// <summary>
  39. /// 生成缩略图
  40. /// </summary>
  41. /// <param name="originalImagePath"></param>
  42. /// <param name="width"></param>
  43. /// <param name="height"></param>
  44. /// <param name="mode"></param>
  45. /// <returns></returns>
  46. public static bool MakeThumbnail(string originalImagePath, int width, int height, ThumbnailMod mode)
  47. {
  48. string thumbnailPath = originalImagePath.Substring(0, originalImagePath.LastIndexOf('.')) + "s.jpg";
  49. Image originalImage = Image.FromFile(originalImagePath);
  50. int towidth = width;
  51. int toheight = height;
  52. int x = 0;
  53. int y = 0;
  54. int ow = originalImage.Width;
  55. int oh = originalImage.Height;
  56. switch (mode)
  57. {
  58. case ThumbnailMod.HW://指定高宽缩放(可能变形)
  59. break;
  60. case ThumbnailMod.W://指定宽,高按比例
  61. toheight = originalImage.Height * width / originalImage.Width;
  62. break;
  63. case ThumbnailMod.H://指定高,宽按比例
  64. towidth = originalImage.Width * height / originalImage.Height;
  65. break;
  66. case ThumbnailMod.Cut://指定高宽裁减(不变形)
  67. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  68. {
  69. oh = originalImage.Height;
  70. ow = originalImage.Height * towidth / toheight;
  71. y = 0;
  72. x = (originalImage.Width - ow) / 2;
  73. }
  74. else
  75. {
  76. ow = originalImage.Width;
  77. oh = originalImage.Width * height / towidth;
  78. x = 0;
  79. y = (originalImage.Height - oh) / 2;
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. //新建一个bmp图片
  86. Image bitmap = new Bitmap(towidth, toheight);
  87. //新建一个画板
  88. Graphics g = Graphics.FromImage(bitmap);
  89. //设置高质量插值法
  90. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  91. //设置高质量,低速度呈现平滑程度
  92. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  93. //清空画布并以透明背景色填充
  94. g.Clear(System.Drawing.Color.Transparent);
  95. //在指定位置并且按指定大小绘制原图片的指定部分
  96. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  97. new Rectangle(x, y, ow, oh),
  98. GraphicsUnit.Pixel);
  99. bool isok = false;
  100. try
  101. {
  102. //以jpg格式保存缩略图
  103. bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
  104. isok = true;
  105. }
  106. catch (Exception)
  107. {
  108. thumbnailPath = originalImagePath;
  109. }
  110. finally
  111. {
  112. originalImage.Dispose();
  113. bitmap.Dispose();
  114. g.Dispose();
  115. }
  116. return isok;
  117. }
  118. #endregion
  119. #region 在图片上生成图片水印
  120. ///// <summary>
  121. ///// 在图片上生成图片水印
  122. ///// </summary>
  123. ///// <param name="Path">原服务器图片路径</param>
  124. ///// <param name="Path_syp">生成的带图片水印的图片路径</param>
  125. ///// <param name="Path_sypf">水印图片路径</param>
  126. /// <summary>
  127. /// 在图片上生成图片水印
  128. /// </summary>
  129. /// <param name="Path">原服务器图片路径</param>
  130. /// <param name="Path_sypf">水印图片路径</param>
  131. public static void AddWaterPic(string Path, string Path_sypf)
  132. {
  133. try
  134. {
  135. Image image = Image.FromFile(Path);
  136. Image copyImage = Image.FromFile(Path_sypf);
  137. Graphics g = Graphics.FromImage(image);
  138. g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
  139. g.Dispose();
  140. image.Save(Path + ".temp");
  141. image.Dispose();
  142. System.IO.File.Delete(Path);
  143. File.Move(Path + ".temp", Path);
  144. }
  145. catch
  146. { }
  147. }
  148. #endregion
  149. /// <summary>
  150. /// 公共方法
  151. /// </summary>
  152. private static void GetImgType()
  153. {
  154. htmimes[".jpe"] = "image/jpeg";
  155. htmimes[".jpeg"] = "image/jpeg";
  156. htmimes[".jpg"] = "image/jpeg";
  157. htmimes[".png"] = "image/png";
  158. htmimes[".tif"] = "image/tiff";
  159. htmimes[".tiff"] = "image/tiff";
  160. htmimes[".bmp"] = "image/bmp";
  161. }
  162. #region 返回新图片尺寸
  163. /// <summary>
  164. /// 返回新图片尺寸
  165. /// </summary>
  166. /// <param name="width">原始宽</param>
  167. /// <param name="height">原始高</param>
  168. /// <param name="maxWidth">新图片最大宽</param>
  169. /// <param name="maxHeight">新图片最大高</param>
  170. /// <returns></returns>
  171. public static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
  172. {
  173. decimal MAX_WIDTH = (decimal)maxWidth;
  174. decimal MAX_HEIGHT = (decimal)maxHeight;
  175. decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
  176. int newWidth, newHeight;
  177. decimal originalWidth = (decimal)width;
  178. decimal originalHeight = (decimal)height;
  179. if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
  180. {
  181. decimal factor;
  182. // determine the largest factor
  183. if (originalWidth / originalHeight > ASPECT_RATIO)
  184. {
  185. factor = originalWidth / MAX_WIDTH;
  186. newWidth = Convert.ToInt32(originalWidth / factor);
  187. newHeight = Convert.ToInt32(originalHeight / factor);
  188. }
  189. else
  190. {
  191. factor = originalHeight / MAX_HEIGHT;
  192. newWidth = Convert.ToInt32(originalWidth / factor);
  193. newHeight = Convert.ToInt32(originalHeight / factor);
  194. }
  195. }
  196. else
  197. {
  198. newWidth = width;
  199. newHeight = height;
  200. }
  201. return new Size(newWidth, newHeight);
  202. }
  203. #endregion
  204. }
  205. }