Asp.net generates high-definition thumbnails)

Source: Internet
Author: User

 

Code
1 /**/ ///   <Summary>  
2 /// Generate a thumbnail
3 ///   </Summary>  
4 ///   <Param name = "originalimagepath"> Source image path (physical path) </Param>  
5 ///   <Param name = "thumbnailpath"> Thumbnail path (physical path) </Param>  
6 ///   <Param name = "width"> Thumbnail width </Param>  
7 ///   <Param name = "height"> Thumbnail height </Param>

8 Public Void Makethumbnail ( String Originalimagepath, String Thumbnailpath, Int Width, Int Height)
9 {
10 System. Drawing. Image originalimage = System. Drawing. image. fromfile (originalimagepath );
11
12 Int Towidth =   0 ;
13 Int Toheight = 0 ;
14 If (Originalimage. Width > Width && Originalimage. Height < Height)
15 {
16Towidth=Width;
17Toheight=Originalimage. height;
18}
 
19
20 If (Originalimage. Width < Width && Originalimage. Height > Height)
21 {
22Towidth=Originalimage. width;
23Toheight=Height;
24}
 
25 If (Originalimage. Width > Width && Originalimage. Height > Height)
26 {
27Towidth=Width;
28Toheight=Height;
29}
 
30 If (Originalimage. Width < Width && Originalimage. Height < Height)
31 {
32Towidth=Originalimage. width;
33Toheight=Originalimage. height;
34}
 
35 Int X =   0 ; // X coordinate in the upper left corner
36 Int Y =   0 ; // Y coordinate in the upper left corner
37
38
39 // Create a BMP Image
40 System. Drawing. Image bitmap =   New System. Drawing. Bitmap (towidth, toheight );
41
42 // Create a canvas
43 Graphics g = System. Drawing. Graphics. fromimage (Bitmap );
44
45 // Set a high quality Interpolation Method
46 G. interpolationmode = System. Drawing. drawing2d. interpolationmode. High;
47
48 // Set high quality, low speed rendering Smoothness
49 G. smoothingmode = System. Drawing. drawing2d. smoothingmode. highquality;
50
51 // Clear the canvas and fill it with a transparent background color
52 G. Clear (color. Transparent );
53
54 // Draw the specified part of the original image at the specified position and in the specified size
55 G. drawimage (originalimage, X, Y, towidth, toheight );
56
57 Try  
58 {
59//Save thumbnails in JPG format
60Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );
61}
 
62 Catch (System. Exception E)
63 {
64ThrowE;
65}
 
66 Finally  
67 {
68Originalimage. Dispose ();
69Bitmap. Dispose ();
70G. Dispose ();
71}
 
72 }

73

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.