Source code for generating thumbnails using ASP. NET

Source: Internet
Author: User
First, let's look at the calling method: Using System;
Using System. Data;
Using System. configuration;
Using System. collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;

Public   Partial   Class Test_default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Generalimage ();
}
Private   Void Generalimage ()
{
String Savepath =   @" Images/123.jpg " ; // Source image path
String Smallpath =   @" Images/small/123.jpg " ; // The thumbnail to be generated
Savepath = Server. mappath (savepath ); // Do not forget this step.
Smallpath = Server. mappath (smallpath ); // Do not forget it here.
YD. Common. imageclass =   New YD. Common. imageclass ();
Imageclass. showthumbnail (savepath, smallpath, 200 , 153 ); // This call
}
}

Class for generating a thumbnail: Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. drawing;
Using System. Drawing. imaging;
Using System. IO;
///   <Summary>
/// Abstract description of imageclass
///   </Summary>
Namespace YD. Common
{
Public   Class Imageclass
{
Public   Bool Thumbnailcallback ()
{
Return   False ;
}
Public   Void Showthumbnail ( String Oldfile, String Newfile, Int White, Int Height)
{
System. Drawing. Image = System. Drawing. image. fromfile (oldfile );
// Obtain the height and width of the source image.
Int Oldh = Image. height;
Int Oldw = Image. width;
Int NEWW, newh;
NEWW = White; newh = Height; // Directly set the height and width of the new image ,,

Try
{
System. Drawing. image. getthumbnailimageabort callb =   New System. Drawing. image. getthumbnailimageabort (thumbnailcallback );
System. Drawing. image BT =   New System. Drawing. Bitmap (NEWW, newh );
System. Drawing. Graphics gr = System. Drawing. Graphics. fromimage (BT );
Gr. Clear (color. White );
Gr. interpolationmode = System. Drawing. drawing2d. interpolationmode. highqualitybicubic;
Gr. drawimage (image, New Rectangle ( 0 , 0 , NEWW, newh ), 0 , 0 , Oldw, oldh, graphicsunit. pixel );
Switch (Oldfile. substring (oldfile. Length -   3 ). Toupper ())
{
Case   " JPG " :
Bt. Save (newfile, imageformat. JPEG );
Break ;
Case   " GIF " :
Bt. Save (newfile, imageformat. GIF );
Break ;
Case   " PNG " :
Bt. Save (newfile, imageformat. PNG );
Break ;
Default :
Bt. Save (newfile, imageformat. JPEG );
Break ;
}
Gr. Dispose ();
Bt. Dispose ();
Image. Dispose ();
}
Catch {}

}
}
}

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.