ASP.NET生成缩略图的代码
ASP.NET生成缩略图,只需你给定要生成的图片路径,生成后存放的服务器路径,生成方式,生成的尺寸,还有生成后的图片类型,即可为你生成合适的缩略图。
ASP.NET中生成缩略图,只需你给定要生成的图片路径,生成后存放的服务器路径,生成方式,生成的尺寸,还有生成后的图片类型,即可为你生成合适的缩略图。先必须导入 using System.Drawing;
代码如下:01. // <summary>
02. /// 生成缩略图
03. /// </summary>
04. /// <param name="originalImagePath">源图路径</param>
05. /// <param name="thumbnailPath">缩略图路径</param>
06. /// <param name="width">缩略图宽度</param>
07. /// <param name="height">缩略图高度</param>
08. /// <param name="mode">生成缩略图的方式:HW指定高宽缩放(可能变形);W指定宽,高按比例 H指定高,宽按比例 Cut指定高宽裁减(不变形)</param>
09. /// <param name="mode">要缩略图保存的格式(gif,jpg,bmp,png) 为空或未知类型都视为jpg</param>
10. public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string imageType)
11. {
12. Image originalImage = Image.FromFile(originalImagePath);
13. int towidth = width;
14. int toheight = height;
15. int x = 0;
16. int y = 0;
17. int ow = originalImage.Width;
18. int oh = originalImage.Height;
19.20. switch (mode)
21. {
22. case "HW"://指定高宽缩放(可能变形)
23. break;
24. case "W"://指定宽,高按比例
25. toheight = originalImage.Height * width / originalImage.Width;
26. break;
27. case "H"://指定高,宽按比例
28. towidth = originalImage.Width * height / originalImage.Height;
29. break;
30. case "Cut"://指定高宽裁减(不变形)
31. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
32. {
33. oh = originalImage.Height;
34. ow = originalImage.Height * towidth / toheight;
35. y = 0;
36. x = (originalImage.Width - ow) / 2;
37. }
38. else
39. {
40. ow = originalImage.Width;
41. oh = originalImage.Width * height / towidth;
42. x = 0;
43. y = (originalImage.Height - oh) / 2;
44. }
45. break;
46. default:
47. break;
48. }
49. //新建一个bmp图片
50. Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
51.52. //新建一个画板
53. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
54.55. //设置高质量插值法
56. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
57.58. //设置高质量,低速度呈现平滑程度
59. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
60.61. //清空画布并以透明背景色填充
62. g.Clear(Color.Transparent);
63.64. //在指定位置并且按指定大小绘制原图片的指定部分
65. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
66. new Rectangle(x, y, ow, oh),
67. GraphicsUnit.Pixel);
68.69. try
70. {
71. //以jpg格式保存缩略图
72. switch (imageType.ToLower())
73. {
74. case "gif":
75. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
76. break;
77. case "jpg":
78. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
79. break;
80. case "bmp":
81. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
82. break;
83. case "png":
84. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
85. break;
86. default:
87. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
88. break;
89. }
90. }
91. catch (System.Exception e)
92. {
93. throw e;
94. }
95. finally
96. {
97. originalImage.Dispose();
98. bitmap.Dispose();
99. g.Dispose();
100. }
101. }
02. /// 生成缩略图
03. /// </summary>
04. /// <param name="originalImagePath">源图路径</param>
05. /// <param name="thumbnailPath">缩略图路径</param>
06. /// <param name="width">缩略图宽度</param>
07. /// <param name="height">缩略图高度</param>
08. /// <param name="mode">生成缩略图的方式:HW指定高宽缩放(可能变形);W指定宽,高按比例 H指定高,宽按比例 Cut指定高宽裁减(不变形)</param>
09. /// <param name="mode">要缩略图保存的格式(gif,jpg,bmp,png) 为空或未知类型都视为jpg</param>
10. public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string imageType)
11. {
12. Image originalImage = Image.FromFile(originalImagePath);
13. int towidth = width;
14. int toheight = height;
15. int x = 0;
16. int y = 0;
17. int ow = originalImage.Width;
18. int oh = originalImage.Height;
19.20. switch (mode)
21. {
22. case "HW"://指定高宽缩放(可能变形)
23. break;
24. case "W"://指定宽,高按比例
25. toheight = originalImage.Height * width / originalImage.Width;
26. break;
27. case "H"://指定高,宽按比例
28. towidth = originalImage.Width * height / originalImage.Height;
29. break;
30. case "Cut"://指定高宽裁减(不变形)
31. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
32. {
33. oh = originalImage.Height;
34. ow = originalImage.Height * towidth / toheight;
35. y = 0;
36. x = (originalImage.Width - ow) / 2;
37. }
38. else
39. {
40. ow = originalImage.Width;
41. oh = originalImage.Width * height / towidth;
42. x = 0;
43. y = (originalImage.Height - oh) / 2;
44. }
45. break;
46. default:
47. break;
48. }
49. //新建一个bmp图片
50. Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
51.52. //新建一个画板
53. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
54.55. //设置高质量插值法
56. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
57.58. //设置高质量,低速度呈现平滑程度
59. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
60.61. //清空画布并以透明背景色填充
62. g.Clear(Color.Transparent);
63.64. //在指定位置并且按指定大小绘制原图片的指定部分
65. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
66. new Rectangle(x, y, ow, oh),
67. GraphicsUnit.Pixel);
68.69. try
70. {
71. //以jpg格式保存缩略图
72. switch (imageType.ToLower())
73. {
74. case "gif":
75. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
76. break;
77. case "jpg":
78. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
79. break;
80. case "bmp":
81. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
82. break;
83. case "png":
84. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
85. break;
86. default:
87. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
88. break;
89. }
90. }
91. catch (System.Exception e)
92. {
93. throw e;
94. }
95. finally
96. {
97. originalImage.Dispose();
98. bitmap.Dispose();
99. g.Dispose();
100. }
101. }