Source code of the instance where ASP.net generates thumbnails

Source: Internet
Author: User

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;
}
Catch (Exception e)
{
ErrMessage = e. Message;
Return false;
}
}
}

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.