CopyCode The Code is as follows: 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. 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 System. Drawing. 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 (string imagefilename)
{
Resourceimage = system. Drawing. image. fromfile (imagefilename );
Errmessage = "";
}
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 System. Drawing. Image getreducedimage (INT width, int height)
{
Try
{
System. Drawing. Image performancedimage;
System. Drawing. image. getthumbnailimageabort callb = new system. Drawing. 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 ilename.jpg </param>
/// <Returns> success returns true; otherwise, false is returned. </returns>
Public bool getreducedimage (INT width, int height, string targetfilepath)
{
Try
{
System. Drawing. Image performancedimage;
System. Drawing. image. getthumbnailimageabort callb = new system. Drawing. image. getthumbnailimageabort (thumbnailcallback );
Performancedimage = resourceimage. getthumbnailimage (width, height, callb, intptr. Zero );
Performancedimage. Save (@ targetfilepath, imageformat. JPEG );
Cecedimage. Dispose ();
Return true;
}
Catch (exception E)
{
Errmessage = E. message;
Return false;
}
}
/// <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 System. Drawing. Image getreducedimage (double percent)
{
Try
{
System. Drawing. Image performancedimage;
System. Drawing. image. getthumbnailimageabort callb = new system. Drawing. 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 ilename.jpg </param>
/// <Returns> success returns true; otherwise, false is returned. </returns>
Public bool getreducedimage (double percent, string targetfilepath)
{
Try
{
System. Drawing. Image performancedimage;
System. Drawing. image. getthumbnailimageabort callb = new system. Drawing. 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;
}< br> catch (exception e)
{< br> errmessage = E. message;
return false;
}< BR >}