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 {}
}
}
}