ImageThumbnailMake.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.IO;
  11. using System.Runtime.InteropServices;
  12. namespace Ant.Service.Common
  13. {
  14. /// <summary>
  15. ///pic_zip 图片缩略图生成类
  16. /// </summary>
  17. public class ImageThumbnailMake
  18. {
  19. /// <summary>
  20. /// 图片缩略图生成
  21. /// </summary>
  22. public ImageThumbnailMake()
  23. {
  24. }
  25. /// <summary>
  26. /// 图片缩略图生成算法
  27. /// </summary>
  28. /// <param name="int_Width">宽度</param>
  29. /// <param name="int_Height">高度</param>
  30. /// <param name="input_ImgFile">文件路径</param>
  31. /// <param name="out_ImgFile">保存文件路径</param>
  32. /// <param name="filename">文件名</param>
  33. /// <returns></returns>
  34. public static bool MakeThumbnail(int int_Width, int int_Height, string input_ImgFile, string out_ImgFile, string filename)
  35. {
  36. System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile + filename);
  37. float New_Width; // 新的宽度
  38. float New_Height; // 新的高度
  39. float Old_Width, Old_Height; //原始高宽
  40. int flat = 0;//标记图片是不是等比
  41. int xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。
  42. int yPoint = 0;
  43. //判断图片
  44. Old_Width = (float)oldimage.Width;
  45. Old_Height = (float)oldimage.Height;
  46. if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height)) //当图片太宽的时候
  47. {
  48. New_Height = Old_Height * ((float)int_Width / (float)Old_Width);
  49. New_Width = (float)int_Width;
  50. //此时x坐标不用修改
  51. yPoint = (int)(((float)int_Height - New_Height) / 2);
  52. flat = 1;
  53. }
  54. else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))
  55. {
  56. New_Width = int_Width;
  57. New_Height = int_Height;
  58. }
  59. else
  60. {
  61. New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height); //太高的时候
  62. New_Height = int_Height;
  63. //此时y坐标不用修改
  64. xPoint = (int)(((float)int_Width - New_Width) / 2);
  65. flat = 1;
  66. }
  67. // System.Drawing.Image.GetThumbnailImageAbort callb = null;
  68. // ===缩小图片===
  69. //调用缩放算法
  70. System.Drawing.Image thumbnailImage = Makesmallimage(oldimage, (int)New_Width, (int)New_Height);
  71. Bitmap bm = new Bitmap(thumbnailImage);
  72. if (flat != 0)
  73. {
  74. Bitmap bmOutput = new Bitmap(int_Width, int_Height);
  75. Graphics gc = Graphics.FromImage(bmOutput);
  76. SolidBrush tbBg = new SolidBrush(Color.White);
  77. gc.FillRectangle(tbBg, 0, 0, int_Width, int_Height); //填充为白色
  78. gc.DrawImage(bm, xPoint, yPoint, (int)New_Width, (int)New_Height);
  79. bmOutput.Save(out_ImgFile + filename);
  80. }
  81. else
  82. {
  83. bm.Save(out_ImgFile + filename);
  84. }
  85. oldimage.Dispose();
  86. return true;
  87. }
  88. /// <summary>
  89. /// 生成缩略图 (高清缩放)
  90. /// </summary>
  91. /// <param name="originalImage">原图片</param>
  92. /// <param name="width">缩放宽度</param>
  93. /// <param name="height">缩放高度</param>
  94. /// <returns></returns>
  95. public static Image Makesmallimage(System.Drawing.Image originalImage, int width, int height)
  96. {
  97. int towidth = 0;
  98. int toheight = 0;
  99. if (originalImage.Width > width && originalImage.Height < height)
  100. {
  101. towidth = width;
  102. toheight = originalImage.Height;
  103. }
  104. if (originalImage.Width < width && originalImage.Height > height)
  105. {
  106. towidth = originalImage.Width;
  107. toheight = height;
  108. }
  109. if (originalImage.Width > width && originalImage.Height > height)
  110. {
  111. towidth = width;
  112. toheight = height;
  113. }
  114. if (originalImage.Width < width && originalImage.Height < height)
  115. {
  116. towidth = originalImage.Width;
  117. toheight = originalImage.Height;
  118. }
  119. int x = 0;//左上角的x坐标
  120. int y = 0;//左上角的y坐标
  121. //新建一个bmp图片
  122. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  123. //新建一个画板
  124. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  125. //设置高质量插值法
  126. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  127. //设置高质量,低速度呈现平滑程度
  128. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  129. //清空画布并以透明背景色填充
  130. g.Clear(Color.Transparent);
  131. //在指定位置并且按指定大小绘制原图片的指定部分
  132. g.DrawImage(originalImage, x, y, towidth, toheight);
  133. originalImage.Dispose();
  134. //bitmap.Dispose();
  135. g.Dispose();
  136. return bitmap;
  137. }
  138. /// <summary>
  139. /// 生成缩略图 (没有补白)
  140. /// </summary>
  141. /// <param name="originalImagePath">源图路径(物理路径)</param>
  142. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  143. /// <param name="width">缩略图宽度</param>
  144. /// <param name="height">缩略图高度</param>
  145. public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
  146. {
  147. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  148. int towidth = 0;
  149. int toheight = 0;
  150. if (originalImage.Width > width && originalImage.Height < height)
  151. {
  152. towidth = width;
  153. toheight = originalImage.Height;
  154. }
  155. if (originalImage.Width < width && originalImage.Height > height)
  156. {
  157. towidth = originalImage.Width;
  158. toheight = height;
  159. }
  160. if (originalImage.Width > width && originalImage.Height > height)
  161. {
  162. towidth = width;
  163. toheight = height;
  164. }
  165. if (originalImage.Width < width && originalImage.Height < height)
  166. {
  167. towidth = originalImage.Width;
  168. toheight = originalImage.Height;
  169. }
  170. int x = 0;//左上角的x坐标
  171. int y = 0;//左上角的y坐标
  172. //新建一个bmp图片
  173. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  174. //新建一个画板
  175. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  176. //设置高质量插值法
  177. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  178. //设置高质量,低速度呈现平滑程度
  179. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  180. //清空画布并以透明背景色填充
  181. g.Clear(Color.Transparent);
  182. //在指定位置并且按指定大小绘制原图片的指定部分
  183. g.DrawImage(originalImage, x, y, towidth, toheight);
  184. try
  185. {
  186. bitmap.Save(thumbnailPath);
  187. }
  188. catch (System.Exception e)
  189. {
  190. throw e;
  191. }
  192. finally
  193. {
  194. originalImage.Dispose();
  195. bitmap.Dispose();
  196. g.Dispose();
  197. }
  198. }
  199. }
  200. }