ASP. to generate a thumbnail, you only need to specify the image path to be generated, the server path to be generated, the generation method, the size of the generated image, and the generated image type, you can generate a suitable thumbnail for you.
ASP. to generate a thumbnail in. net, you only need to specify the image path to be generated, the server path to be generated, the generation method, the size of the generated image, and the generated image type, you can generate a suitable thumbnail for you. You must first import using system. drawing;
The Code is as follows:
01. // <summary>
02. // generate a thumbnail
03. // </Summary>
04. // <Param name = "originalimagepath"> source image path </param>
05. // <Param name = "thumbnailpath"> thumbnail path </param>
06. // <Param name = "width"> thumbnail width </param>
07. // <Param name = "height"> thumbnail height </param>
08. /// <Param name = "Mode"> how to generate a thumbnail: HW specifies the High-width Scaling (may be deformed); W specifies the width, and H specifies the height based on the ratio, width proportional cut specifies high width cut (not deformed) </param>
09. // <Param name = "Mode"> the format (GIF, JPG, BMP, and PNG) to be saved in a thumbnail is null or an unknown type. 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": // specify high-width Scaling (possibly deformed)
23. Break;
24. Case "W": // specify the width, and the height is proportional.
25. toheight = originalimage. Height * width/originalimage. width;
26. Break;
27. Case "H": // specify the height, width by proportion
28. towidth = originalimage. Width * Height/originalimage. height;
29. Break;
30. Case "cut": // specify the height and width of the cut (not deformed)
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. // create a new BMP Image
50. Image bitmap = new system. Drawing. Bitmap (towidth, toheight );
51.52. // create a canvas
53. Graphics G = system. Drawing. Graphics. fromimage (Bitmap );
54.55. // set a high quality Interpolation Method
56. G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High;
57.58. // set high quality and smooth Low Speed
59. G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;
60.61. // clear the canvas and fill it with a transparent background color
62. G. Clear (color. Transparent );
63.64. // draw the specified part of the original image at the specified position and in the specified size
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. // Save the thumbnail in JPG format
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 .}