Asp.net generates the thumbnail code

Source: Internet
Author: User

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

Related Article

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.