CutThumbnail.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Drawing;
  2. namespace HistoryDLL
  3. {
  4. enum SelectType
  5. {
  6. //按寬高 (可能變形)
  7. HW = 0x0001,
  8. //指定寬, 高按比例改變
  9. W = 0x0010,
  10. //指定高, 寬按比例改變
  11. H = 0x0100,
  12. //指定高寬進行裁減(不變形)
  13. Cut = 0x1000
  14. };
  15. public class CutThumbnail
  16. {
  17. public CutThumbnail(string imagepath, int cutWidth, int cutHeight, string imageFormat, string ThumbPath)
  18. {
  19. MakeThumbnail(imagepath, ThumbPath, cutWidth, cutHeight, SelectType.Cut, imageFormat);
  20. }
  21. private void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, SelectType mode, string imageFormat)
  22. {
  23. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  24. int towidth = (int)width;
  25. int toheight = (int)height;
  26. int x = 0;
  27. int y = 0;
  28. int ow = originalImage.Width;
  29. int oh = originalImage.Height;
  30. switch (mode)
  31. {
  32. case SelectType.HW://指定高宽缩放(可能变形)
  33. break;
  34. case SelectType.W://指定宽,高按比例
  35. toheight = originalImage.Height * width / originalImage.Width;
  36. break;
  37. case SelectType.H://指定高,宽按比例
  38. towidth = originalImage.Width * height / originalImage.Height;
  39. break;
  40. case SelectType.Cut://指定高宽裁减(不变形)
  41. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  42. {
  43. oh = originalImage.Height;
  44. ow = originalImage.Height * towidth / toheight;
  45. y = 0;
  46. x = (originalImage.Width - ow) / 2;
  47. }
  48. else
  49. {
  50. ow = originalImage.Width;
  51. oh = originalImage.Width * height / towidth;
  52. x = 0;
  53. y = (originalImage.Height - oh) / 2;
  54. }
  55. break;
  56. default:
  57. break;
  58. }
  59. //新建一个bmp图片
  60. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  61. //新建一个画板
  62. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  63. //设置高质量插值法
  64. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  65. //设置高质量,低速度呈现平滑程度
  66. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  67. //清空画布并以透明背景色填充
  68. g.Clear(System.Drawing.Color.Transparent);
  69. //在指定位置并且按指定大小绘制原图片的指定部分
  70. g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
  71. try
  72. {
  73. System.Drawing.Imaging.ImageFormat imgtype = System.Drawing.Imaging.ImageFormat.Jpeg;
  74. if (imageFormat == "jpg")
  75. {
  76. imgtype = System.Drawing.Imaging.ImageFormat.Jpeg;
  77. }
  78. //保存缩略图
  79. bitmap.Save(thumbnailPath, imgtype);
  80. }
  81. catch (System.Exception e)
  82. {
  83. throw e;
  84. }
  85. finally
  86. {
  87. originalImage.Dispose();
  88. bitmap.Dispose();
  89. g.Dispose();
  90. }
  91. }
  92. }
  93. }