RotateImageTool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Azylee.Core.IOUtils.ExifUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Azylee.Core.IOUtils.ImageUtils
  8. {
  9. public static class RotateImageTool
  10. {
  11. /// <summary>
  12. /// 旋转照片(根据Exif信息的方向)
  13. /// </summary>
  14. /// <param name="originalImagePath"></param>
  15. /// <param name="saveImagePath"></param>
  16. /// <returns></returns>
  17. public static bool Rotate(string originalImagePath, string saveImagePath)
  18. {
  19. Image img = Image.FromFile(originalImagePath);
  20. var exif = img.PropertyItems;
  21. byte orien = 0;
  22. var item = exif.Where(m => m.Id == 274).ToArray();
  23. if (item.Length > 0)
  24. orien = item[0].Value[0];
  25. switch (orien)
  26. {
  27. case 2:
  28. img.RotateFlip(RotateFlipType.RotateNoneFlipX);//horizontal flip
  29. break;
  30. case 3:
  31. img.RotateFlip(RotateFlipType.Rotate180FlipNone);//right-top
  32. break;
  33. case 4:
  34. img.RotateFlip(RotateFlipType.RotateNoneFlipY);//vertical flip
  35. break;
  36. case 5:
  37. img.RotateFlip(RotateFlipType.Rotate90FlipX);
  38. break;
  39. case 6:
  40. img.RotateFlip(RotateFlipType.Rotate90FlipNone);//right-top
  41. break;
  42. case 7:
  43. img.RotateFlip(RotateFlipType.Rotate270FlipX);
  44. break;
  45. case 8:
  46. img.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottom
  47. break;
  48. default:
  49. break;
  50. }
  51. try
  52. {
  53. img.Save(saveImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  54. return true;
  55. }
  56. catch (Exception e) { return false; }
  57. finally
  58. {
  59. img.Dispose();
  60. }
  61. }
  62. /// <summary>
  63. /// 逆时针旋转图像
  64. /// </summary>
  65. /// <param name="originalImagePath">原始图像路径</param>
  66. /// <param name="saveImagePath">保存图像的路径</param>
  67. /// <param name = "angle" > 旋转角度[0, 360](前台给的) </ param >
  68. /// <returns></returns>
  69. public static bool Rotate(string originalImagePath, string saveImagePath, int angle)
  70. {
  71. Image originalImage = Image.FromFile(originalImagePath);
  72. angle = angle % 360;
  73. //弧度转换
  74. double radian = angle * Math.PI / 180.0;
  75. double cos = Math.Cos(radian);
  76. double sin = Math.Sin(radian);
  77. //原图的宽和高
  78. int w = originalImage.Width;
  79. int h = originalImage.Height;
  80. int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
  81. int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
  82. //目标位图
  83. Bitmap saveImage = new Bitmap(W, H);
  84. Graphics g = Graphics.FromImage(saveImage);
  85. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
  86. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  87. //计算偏移量
  88. Point Offset = new Point((W - w) / 2, (H - h) / 2);
  89. //构造图像显示区域:让图像的中心与窗口的中心点一致
  90. Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
  91. Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
  92. g.TranslateTransform(center.X, center.Y);
  93. g.RotateTransform(360 - angle);
  94. //恢复图像在水平和垂直方向的平移
  95. g.TranslateTransform(-center.X, -center.Y);
  96. g.DrawImage(originalImage, rect);
  97. //重至绘图的所有变换
  98. g.ResetTransform();
  99. g.Save();
  100. //保存旋转后的图片
  101. originalImage.Dispose();
  102. try
  103. {
  104. saveImage.Save(saveImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  105. return true;
  106. }
  107. catch (Exception e) { return false; }
  108. finally
  109. {
  110. originalImage.Dispose();
  111. saveImage.Dispose();
  112. g.Dispose();
  113. }
  114. }
  115. }
  116. }