ImageHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Drawing;
  3. namespace Y.Utils.ImageUtils
  4. {
  5. public class ImageHelper
  6. {
  7. /// <summary>
  8. /// 生成缩略图
  9. /// </summary>
  10. /// <param name="originalImagePath">源图路径(物理路径)</param>
  11. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  12. /// <param name="width">缩略图宽度</param>
  13. /// <param name="height">缩略图高度</param>
  14. /// <param name="mode">生成缩略图的方式</param>
  15. public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  16. {
  17. Image originalImage = Image.FromFile(originalImagePath);
  18. int towidth = width;
  19. int toheight = height;
  20. int x = 0;
  21. int y = 0;
  22. int ow = originalImage.Width;
  23. int oh = originalImage.Height;
  24. switch (mode)
  25. {
  26. case "HW"://指定高宽缩放(可能变形)
  27. break;
  28. case "W"://指定宽,高按比例
  29. toheight = originalImage.Height * width / originalImage.Width;
  30. break;
  31. case "H"://指定高,宽按比例
  32. towidth = originalImage.Width * height / originalImage.Height;
  33. break;
  34. case "Cut"://指定高宽裁减(不变形)
  35. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  36. {
  37. oh = originalImage.Height;
  38. ow = originalImage.Height * towidth / toheight;
  39. y = 0;
  40. x = (originalImage.Width - ow) / 2;
  41. }
  42. else
  43. {
  44. ow = originalImage.Width;
  45. oh = originalImage.Width * height / towidth;
  46. x = 0;
  47. y = (originalImage.Height - oh) / 2;
  48. }
  49. break;
  50. default:
  51. break;
  52. }
  53. //新建一个bmp图片
  54. Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  55. //新建一个画板
  56. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  57. //设置高质量插值法
  58. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  59. //设置高质量,低速度呈现平滑程度
  60. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  61. //清空画布并以透明背景色填充
  62. g.Clear(Color.Transparent);
  63. //在指定位置并且按指定大小绘制原图片的指定部分
  64. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  65. new Rectangle(x, y, ow, oh),
  66. GraphicsUnit.Pixel);
  67. try
  68. {
  69. //以jpg格式保存缩略图
  70. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  71. return true;
  72. }
  73. catch (System.Exception e)
  74. {
  75. return false;
  76. //throw e;
  77. }
  78. finally
  79. {
  80. originalImage.Dispose();
  81. bitmap.Dispose();
  82. g.Dispose();
  83. }
  84. }
  85. /// <summary>
  86. /// 逆时针旋转图像
  87. /// </summary>
  88. /// <param name="originalImagePath">原始图像路径</param>
  89. /// <param name="saveImagePath">保存图像的路径</param>
  90. /// <param name = "angle" > 旋转角度[0, 360](前台给的) </ param >
  91. /// <returns></returns>
  92. public static bool RotateImg(string originalImagePath, string saveImagePath, int angle)
  93. {
  94. Image originalImage = Image.FromFile(originalImagePath);
  95. angle = angle % 360;
  96. //弧度转换
  97. double radian = angle * Math.PI / 180.0;
  98. double cos = Math.Cos(radian);
  99. double sin = Math.Sin(radian);
  100. //原图的宽和高
  101. int w = originalImage.Width;
  102. int h = originalImage.Height;
  103. int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
  104. int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
  105. //目标位图
  106. Bitmap saveImage = new Bitmap(W, H);
  107. Graphics g = Graphics.FromImage(saveImage);
  108. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
  109. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  110. //计算偏移量
  111. Point Offset = new Point((W - w) / 2, (H - h) / 2);
  112. //构造图像显示区域:让图像的中心与窗口的中心点一致
  113. Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
  114. Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
  115. g.TranslateTransform(center.X, center.Y);
  116. g.RotateTransform(360 - angle);
  117. //恢复图像在水平和垂直方向的平移
  118. g.TranslateTransform(-center.X, -center.Y);
  119. g.DrawImage(originalImage, rect);
  120. //重至绘图的所有变换
  121. g.ResetTransform();
  122. g.Save();
  123. //保存旋转后的图片
  124. originalImage.Dispose();
  125. try
  126. {
  127. saveImage.Save(saveImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  128. return true;
  129. }
  130. catch (Exception e) { return false; }
  131. finally
  132. {
  133. originalImage.Dispose();
  134. saveImage.Dispose();
  135. g.Dispose();
  136. }
  137. }
  138. }
  139. }