C # class for generating image thumbnails

Source: Internet
Author: User

There are four overload methods in it, some that directly return the image object, some that generate thumbnails, and save them to the specified directory!

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 (string imagefilename)
...{
Resourceimage = 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 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 ilename.jpg </param>
/// <Returns> success returns true; otherwise, false is returned. </returns>
Public bool getreducedimage (INT width, int height, string targetfilepath)
...{
Try
...{
Image cecedimage;
Image. getthumbnailimageabort callb = new 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 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 ilename.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;
}
}
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/useruse/archive/2008/05/19/2458751.aspx

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.