ImageTool.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. namespace Y.Utils.Net20.ImageUtils
  6. {
  7. public class ImageTool
  8. {
  9. /// <summary>
  10. /// 生成缩略图
  11. /// </summary>
  12. /// <param name="originalImagePath">源图路径(物理路径)</param>
  13. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  14. /// <param name="width">缩略图宽度</param>
  15. /// <param name="height">缩略图高度</param>
  16. /// <param name="mode">生成缩略图的方式</param>
  17. public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  18. {
  19. Image originalImage = Image.FromFile(originalImagePath);
  20. int towidth = width;
  21. int toheight = height;
  22. int x = 0;
  23. int y = 0;
  24. int ow = originalImage.Width;
  25. int oh = originalImage.Height;
  26. switch (mode)
  27. {
  28. case "HW"://指定高宽缩放(可能变形)
  29. break;
  30. case "W"://指定宽,高按比例
  31. toheight = originalImage.Height * width / originalImage.Width;
  32. break;
  33. case "H"://指定高,宽按比例
  34. towidth = originalImage.Width * height / originalImage.Height;
  35. break;
  36. case "Cut"://指定高宽裁减(不变形)
  37. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  38. {
  39. oh = originalImage.Height;
  40. ow = originalImage.Height * towidth / toheight;
  41. y = 0;
  42. x = (originalImage.Width - ow) / 2;
  43. }
  44. else
  45. {
  46. ow = originalImage.Width;
  47. oh = originalImage.Width * height / towidth;
  48. x = 0;
  49. y = (originalImage.Height - oh) / 2;
  50. }
  51. break;
  52. default:
  53. break;
  54. }
  55. //新建一个bmp图片
  56. Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  57. //新建一个画板
  58. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  59. //设置高质量插值法
  60. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  61. //设置高质量,低速度呈现平滑程度
  62. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  63. //清空画布并以透明背景色填充
  64. g.Clear(Color.Transparent);
  65. //在指定位置并且按指定大小绘制原图片的指定部分
  66. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  67. new Rectangle(x, y, ow, oh),
  68. GraphicsUnit.Pixel);
  69. try
  70. {
  71. //以jpg格式保存缩略图
  72. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  73. return true;
  74. }
  75. catch (System.Exception e)
  76. {
  77. return false;
  78. //throw e;
  79. }
  80. finally
  81. {
  82. originalImage.Dispose();
  83. bitmap.Dispose();
  84. g.Dispose();
  85. }
  86. }
  87. /// <summary>
  88. /// 逆时针旋转图像
  89. /// </summary>
  90. /// <param name="originalImagePath">原始图像路径</param>
  91. /// <param name="saveImagePath">保存图像的路径</param>
  92. /// <param name = "angle" > 旋转角度[0, 360](前台给的) </ param >
  93. /// <returns></returns>
  94. public static bool RotateImg(string originalImagePath, string saveImagePath, int angle)
  95. {
  96. Image originalImage = Image.FromFile(originalImagePath);
  97. angle = angle % 360;
  98. //弧度转换
  99. double radian = angle * Math.PI / 180.0;
  100. double cos = Math.Cos(radian);
  101. double sin = Math.Sin(radian);
  102. //原图的宽和高
  103. int w = originalImage.Width;
  104. int h = originalImage.Height;
  105. int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
  106. int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
  107. //目标位图
  108. Bitmap saveImage = new Bitmap(W, H);
  109. Graphics g = Graphics.FromImage(saveImage);
  110. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
  111. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  112. //计算偏移量
  113. Point Offset = new Point((W - w) / 2, (H - h) / 2);
  114. //构造图像显示区域:让图像的中心与窗口的中心点一致
  115. Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
  116. Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
  117. g.TranslateTransform(center.X, center.Y);
  118. g.RotateTransform(360 - angle);
  119. //恢复图像在水平和垂直方向的平移
  120. g.TranslateTransform(-center.X, -center.Y);
  121. g.DrawImage(originalImage, rect);
  122. //重至绘图的所有变换
  123. g.ResetTransform();
  124. g.Save();
  125. //保存旋转后的图片
  126. originalImage.Dispose();
  127. try
  128. {
  129. saveImage.Save(saveImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  130. return true;
  131. }
  132. catch (Exception e) { return false; }
  133. finally
  134. {
  135. originalImage.Dispose();
  136. saveImage.Dispose();
  137. g.Dispose();
  138. }
  139. }
  140. /// 无损压缩图片
  141. /// <param name="sFile">原图片</param>
  142. /// <param name="dFile">压缩后保存位置</param>
  143. /// <param name="dHeight">高度</param>
  144. /// <param name="dWidth"></param>
  145. /// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>
  146. /// <returns></returns>
  147. public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
  148. {
  149. Image iSource = Image.FromFile(sFile);
  150. ImageFormat tFormat = iSource.RawFormat;
  151. int sW = 0, sH = 0;
  152. //按比例缩放
  153. Size tem_size = new Size(iSource.Width, iSource.Height);
  154. if (tem_size.Width > dHeight || tem_size.Width > dWidth)
  155. {
  156. if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
  157. {
  158. sW = dWidth;
  159. sH = (dWidth * tem_size.Height) / tem_size.Width;
  160. }
  161. else
  162. {
  163. sH = dHeight;
  164. sW = (tem_size.Width * dHeight) / tem_size.Height;
  165. }
  166. }
  167. else
  168. {
  169. sW = tem_size.Width;
  170. sH = tem_size.Height;
  171. }
  172. Bitmap ob = new Bitmap(dWidth, dHeight);
  173. Graphics g = Graphics.FromImage(ob);
  174. g.Clear(Color.WhiteSmoke);
  175. g.CompositingQuality = CompositingQuality.HighQuality;
  176. g.SmoothingMode = SmoothingMode.HighQuality;
  177. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  178. g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
  179. g.Dispose();
  180. //以下代码为保存图片时,设置压缩质量
  181. EncoderParameters ep = new EncoderParameters();
  182. long[] qy = new long[1];
  183. qy[0] = flag;//设置压缩的比例1-100
  184. EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
  185. ep.Param[0] = eParam;
  186. try
  187. {
  188. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
  189. ImageCodecInfo jpegICIinfo = null;
  190. for (int x = 0; x < arrayICI.Length; x++)
  191. {
  192. if (arrayICI[x].FormatDescription.Equals("JPEG"))
  193. {
  194. jpegICIinfo = arrayICI[x];
  195. break;
  196. }
  197. }
  198. if (jpegICIinfo != null)
  199. {
  200. ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
  201. }
  202. else
  203. {
  204. ob.Save(dFile, tFormat);
  205. }
  206. return true;
  207. }
  208. catch
  209. {
  210. return false;
  211. }
  212. finally
  213. {
  214. iSource.Dispose();
  215. ob.Dispose();
  216. }
  217. }
  218. }
  219. }