Helper_Thumbnails.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.IO;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using System.Drawing;
  17. using System.Drawing.Drawing2D;
  18. using System.Drawing.Imaging;
  19. namespace iNethinkCMS.Helper
  20. {
  21. public class Helper_Thumbnails
  22. {
  23. /// <summary>
  24. /// 图片缩放
  25. /// </summary>
  26. /// <param name="originalImagePath">原始图片路径</param>
  27. /// <param name="thumbnailPath">生成缩略图图片路径</param>
  28. /// <param name="width"></param>
  29. /// <param name="height"></param>
  30. /// <param name="mode">1,以最大高/宽,等比例缩放 2,缩放至指定高宽(可能变形) 3,以宽为标准,高自动适应进行缩放 4,以高为标准,宽自动适应进行缩放 5,以高宽为标准,进行裁切</param>
  31. /// <param name="mode">图片清晰度 1-100</param>
  32. public static void CreationThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, int quality)
  33. {
  34. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  35. int newWidth = width;
  36. int newHeight = height;
  37. int x = 0;
  38. int y = 0;
  39. int ow = originalImage.Width;
  40. int oh = originalImage.Height;
  41. if (mode == "1")
  42. {
  43. if (height * ow > width * oh)
  44. {
  45. mode = "3";
  46. }
  47. else
  48. {
  49. mode = "4";
  50. }
  51. }
  52. switch (mode)
  53. {
  54. case "2":
  55. break;
  56. case "3":
  57. newHeight = originalImage.Height * width / originalImage.Width;
  58. break;
  59. case "4":
  60. newWidth = originalImage.Width * height / originalImage.Height;
  61. break;
  62. case "5":
  63. if ((double)originalImage.Width / (double)originalImage.Height > (double)newWidth / (double)newHeight)
  64. {
  65. oh = originalImage.Height;
  66. ow = originalImage.Height * newWidth / newHeight;
  67. y = 0;
  68. x = (originalImage.Width - ow) / 2;
  69. }
  70. else
  71. {
  72. ow = originalImage.Width;
  73. oh = originalImage.Width * height / newWidth;
  74. x = 0;
  75. y = (originalImage.Height - oh) / 2;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight);
  82. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  83. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  84. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  85. g.Clear(System.Drawing.Color.Transparent);
  86. g.DrawImage(originalImage,
  87. new System.Drawing.Rectangle(0, 0, newWidth, newHeight),
  88. new System.Drawing.Rectangle(x, y, ow, oh),
  89. System.Drawing.GraphicsUnit.Pixel
  90. );
  91. System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
  92. ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
  93. System.Drawing.Imaging.ImageCodecInfo ici = GetImageEncodersInfo(Path.GetExtension(originalImagePath));
  94. try
  95. {
  96. bitmap.Save(thumbnailPath, ici, ep);
  97. }
  98. catch (System.Exception e)
  99. {
  100. throw e;
  101. }
  102. finally
  103. {
  104. ep.Dispose();
  105. originalImage.Dispose();
  106. bitmap.Dispose();
  107. g.Dispose();
  108. }
  109. }
  110. /// <summary>
  111. /// 根据文件扩展名取得图片的编码信息
  112. /// </summary>
  113. /// <returns></returns>
  114. private static ImageCodecInfo GetImageEncodersInfo(string byExt)
  115. {
  116. switch (byExt)
  117. {
  118. case ".jpg":
  119. return ImageCodecInfo.GetImageEncoders()[1];
  120. case ".gif":
  121. return ImageCodecInfo.GetImageEncoders()[2];
  122. case ".png":
  123. return ImageCodecInfo.GetImageEncoders()[4];
  124. case ".bmp":
  125. return ImageCodecInfo.GetImageEncoders()[0];
  126. default:
  127. return ImageCodecInfo.GetImageEncoders()[1];
  128. }
  129. }
  130. }
  131. }