There are a total of four methods (also called functions), which were originally used to generate thumbnails. It is not clear after generation, not good! So I added the code to optimize the thumbnail. It is added only to the second method. It can be compared with other methods.
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. drawing;
Using system. Drawing. imaging;
/// <Summary>
/// Image Processing
/// 1. Generate a thumbnail or change the image size and image quality in proportion.
/// 2. Place the generated thumbnail in the specified directory.
/// </Summary>
Public class imageclass
{
Public Image resourceimage;
Private int imagewidth;
Private int imageheight;
Public String errmessage;
/// <Summary>
/// Constructor of the class
/// </Summary>
/// <Param name = "imagefilename"> full path name of the image file </param>
Public imageclass ()
{
}
Public bool thumbnailcallback ()
{
Return false;
}
/// <Summary>
/// Method 1 for generating thumbnail overloading. The image object of the thumbnail is returned.
/// </Summary>
/// <Param name = "width"> width of the thumbnail </param>
/// <Param name = "height"> height of the thumbnail </param>
/// <Returns> thumbnail image object </returns>
Public Image getreducedimage (INT width, int height)
{
Try
{
Image cecedimage;
Image. getthumbnailimageabort callb = new image. getthumbnailimageabort (thumbnailcallback );
Performancedimage = resourceimage. getthumbnailimage (width, height, callb, intptr. Zero );
Return reducedimage;
}
Catch (exception E)
{
Errmessage = E. message;
Return NULL;
}
}
/// <Summary>
/// Method 2 for generating thumbnail overloading. Save the thumbnail file to the specified path.
/// </Summary>
/// <Param name = "width"> width of the thumbnail </param>
/// <Param name = "height"> height of the thumbnail </param>
/// <Param name = "targetfilepath"> full file name saved by the thumbnail (with Path). Parameter format: D: \ images \ filename.jpg </param>
/// <Returns> success returns true; otherwise, false is returned. </returns>
Public bool getreducedimage (INT width, int height, string imagefilename, string targetfilepath)
{
Image performancedimage = image. fromfile (imagefilename );
// Create a BMP Image
System. Drawing. Image bitmap = new system. Drawing. Bitmap (width, height );
// Create a canvas
Graphics G = system. Drawing. Graphics. fromimage (Bitmap );
// Set a high quality Interpolation Method
G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High;
// Set high quality and smooth Low Speed
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;
// Clear the canvas and fill it with a transparent background color
G. Clear (color. Transparent );
// Draw the specified part of the original image at the specified position and in the specified size
G. drawimage (performancedimage, new rectangle (0, 0, width, height ),
New rectangle (0, 0, reducedimage. Width, cecedimage. Height ),
Graphicsunit. pixel );
Try
{
// Save the thumbnail in JPG format
Bitmap. Save (targetfilepath, system. Drawing. imaging. imageformat. JPEG );
Return true;
}
Catch (system. Exception E)
{
Throw E;
}
Finally
{
Cecedimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}
/// <Summary>
/// Method 3 for generating thumbnail overloading. The image object of the thumbnail is returned.
/// </Summary>
/// <Param name = "percent"> set the width percentage of the thumbnail to 0.8 if the width percentage is 80. </param>
/// <Returns> thumbnail image object </returns>
Public Image getreducedimage (double percent)
{
Try
{
Image cecedimage;
Image. getthumbnailimageabort callb = new image. getthumbnailimageabort (thumbnailcallback );
Imagewidth = convert. toint32 (resourceimage. Width * percent );
Imageheight = convert. toint32 (resourceimage. Width * percent );
Performancedimage = resourceimage. getthumbnailimage (imagewidth, imageheight, callb, intptr. Zero );
Return reducedimage;
}
Catch (exception E)
{
Errmessage = E. message;
Return NULL;
}
}
/// <Summary>
/// Method 4 for generating thumbnail overloading. The image object of the thumbnail is returned.
/// </Summary>
/// <Param name = "percent"> set the width percentage of the thumbnail to 0.8 if the width percentage is 80. </param>
/// <Param name = "targetfilepath"> full file name saved by the thumbnail (with Path). Parameter format: D: \ images \ filename.jpg </param>
/// <Returns> success returns true; otherwise, false is returned. </returns>
Public bool getreducedimage (double percent, string targetfilepath)
{
Try
{
Image cecedimage;
Image. getthumbnailimageabort callb = new image. getthumbnailimageabort (thumbnailcallback );
Imagewidth = convert. toint32 (resourceimage. Width * percent );
Imageheight = convert. toint32 (resourceimage. Width * percent );
Performancedimage = resourceimage. getthumbnailimage (imagewidth, imageheight, callb, intptr. Zero );
Performancedimage. Save (@ targetfilepath, imageformat. JPEG );
Cecedimage. Dispose ();
Return true;
}
Catch (exception E)
{
Errmessage = E. message;
Return false;
}
}
}
Use strbigimage to represent the full path and the thumbnail address of the file name. strthumbimage indicates the full path and the generated file name of the file name.
Imageclass imgclass = new imageclass ();
Imgclass. getreducedimage (180,180, strbigimage, strthumbimage );