ReduceImageResolution.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace BellwetherBackend.Utility
  11. {
  12. public class ReduceImageResolution
  13. {
  14. private static ImageCodecInfo imageCodecInfo;
  15. private static System.Drawing.Imaging.Encoder myEncoder;
  16. private static EncoderParameter myEncoderParameter;
  17. private static EncoderParameters myEncoderParameters;
  18. //所要縮圖的目標寬高~
  19. private static int TargetWidth = 950;
  20. private static int TargetHeight = 620;
  21. private static int TransRateWidth = 0;
  22. private static int TransRateHeight = 0;
  23. private static float GetImageResolutionHeight(Bitmap bit)
  24. {
  25. return bit.HorizontalResolution;
  26. }
  27. private static float GetImageResolutionWidth(Bitmap bit)
  28. {
  29. return bit.VerticalResolution;
  30. }
  31. private static long GetImageDepth(Bitmap bit)
  32. {
  33. long result = 0;
  34. switch (bit.PixelFormat)
  35. {
  36. case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
  37. {
  38. result = 8;
  39. break;
  40. }
  41. case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
  42. {
  43. result = 24;
  44. break;
  45. }
  46. case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
  47. case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
  48. {
  49. result = 32;
  50. break;
  51. }
  52. }
  53. return result;
  54. }
  55. private static ImageCodecInfo GetEncoderInfo(String mimeType)
  56. {
  57. int j;
  58. ImageCodecInfo[] encoders;
  59. encoders = ImageCodecInfo.GetImageEncoders();
  60. for (j = 0; j < encoders.Length; ++j)
  61. {
  62. if (encoders[j].MimeType == mimeType)
  63. {
  64. return encoders[j];
  65. }
  66. }
  67. return null;
  68. }
  69. private static ImageCodecInfo GetImageCodec(string path)
  70. {
  71. string str = "jpeg";
  72. switch (System.IO.Path.GetExtension(path).ToLower())
  73. {
  74. case ".bmp":
  75. {
  76. str = "bmp";
  77. break;
  78. }
  79. case ".gif":
  80. {
  81. str = "gif";
  82. break;
  83. }
  84. case ".tiff":
  85. {
  86. str = "tiff";
  87. break;
  88. }
  89. case ".png":
  90. {
  91. str = "png";
  92. break;
  93. }
  94. }
  95. return GetEncoderInfo("image/" + str);
  96. }
  97. private void CheckMediaResolution(string fileFrom, string fileTo)
  98. {
  99. File.Copy(fileFrom, fileTo);
  100. }
  101. public static void CheckImageResolution(string fileFrom, string fileTo, int resolutionWidth, int resolutionHeight)
  102. {
  103. TargetWidth = resolutionWidth;
  104. TargetHeight = resolutionHeight;
  105. myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
  106. myEncoderParameters = new EncoderParameters(1);
  107. try
  108. {
  109. if (File.Exists(fileFrom))
  110. {
  111. Bitmap bit = new Bitmap(fileFrom);
  112. //先將原本圖片的影像深度及 Dpi 存起來
  113. float xDpi = GetImageResolutionWidth(bit);
  114. float yDpi = GetImageResolutionHeight(bit);
  115. long depth = GetImageDepth(bit);
  116. imageCodecInfo = GetImageCodec(fileFrom);
  117. TransRateWidth = bit.Width;
  118. TransRateHeight = bit.Height;
  119. if (bit.Width >= bit.Height)
  120. {
  121. if (TransRateWidth > TargetWidth || TransRateHeight > TargetHeight)
  122. {
  123. GetTargetSize(bit);
  124. }
  125. }
  126. else
  127. {
  128. if (TransRateWidth > TargetHeight || TransRateHeight > TargetWidth)
  129. {
  130. GetTargetSize(bit);
  131. }
  132. }
  133. //取得新的 bitmap
  134. bit = KiResizeImage(bit, TransRateWidth, TransRateHeight);
  135. //依照原本的影像深度及DPI來重新設定新圖示的影像深度 及 DPI
  136. bit.SetResolution(xDpi, yDpi);
  137. myEncoderParameter = new EncoderParameter(myEncoder, depth);
  138. myEncoderParameters.Param[0] = myEncoderParameter;
  139. //產生圖片
  140. bit.Save(fileTo, imageCodecInfo, myEncoderParameters);
  141. bit.Dispose();
  142. }
  143. }
  144. catch (Exception)
  145. {
  146. }
  147. }
  148. private static void GetTargetSize(Bitmap bmp)
  149. {
  150. //必須考慮兩種狀況 (橫或者直)
  151. if (bmp.Width >= bmp.Height)
  152. {
  153. TransRateWidth = TargetWidth;
  154. TransRateHeight = TargetHeight;
  155. //橫
  156. //第二考慮 : 往哪個方向縮
  157. if (bmp.Width >= TransRateWidth)
  158. {
  159. decimal result = Math.Round((decimal)TransRateWidth / bmp.Width, 5);
  160. //寬比高大~代表為橫式~縮圖為 : 設定的寬, 設定的高 / 寬的縮圖比例
  161. TransRateHeight = (int)(bmp.Height * result);
  162. }
  163. else if (bmp.Height >= TransRateHeight)
  164. {
  165. decimal result = Math.Round((decimal)TransRateHeight / bmp.Height, 5);
  166. //高比寬大~代表為直式~縮圖為 : 設定的寬 / 高的縮圖比例, 設定的高
  167. TransRateWidth = (int)(bmp.Width * result);
  168. }
  169. }
  170. else if (bmp.Height > bmp.Width)
  171. {
  172. TransRateWidth = TargetHeight;
  173. TransRateHeight = TargetWidth;
  174. //直
  175. //第二考慮 : 往哪個方向縮
  176. if (bmp.Width >= TransRateWidth)
  177. {
  178. decimal result = Math.Round((decimal)TransRateWidth / bmp.Width, 5);
  179. //寬比高大~代表為橫式~縮圖為 : 設定的寬, 設定的高 / 寬的縮圖比例
  180. TransRateHeight = (int)(bmp.Height * result);
  181. }
  182. else if (bmp.Height >= TransRateHeight)
  183. {
  184. decimal result = Math.Round((decimal)TransRateHeight / bmp.Height, 5);
  185. //高比寬大~代表為直式~縮圖為 : 設定的寬 / 高的縮圖比例, 設定的高
  186. TransRateWidth = (int)(bmp.Width * result);
  187. }
  188. }
  189. }
  190. public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
  191. {
  192. try
  193. {
  194. Bitmap b = new Bitmap(newW, newH);
  195. Graphics g = Graphics.FromImage(b);
  196. // 插值算法的質量
  197. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  198. g.CompositingMode = CompositingMode.SourceCopy;
  199. g.Clear(Color.Transparent);
  200. g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newW, newH), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  201. g.Dispose();
  202. bmp.Dispose();
  203. bmp = null;
  204. return b;
  205. }
  206. catch
  207. {
  208. return null;
  209. }
  210. }
  211. }
  212. }