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