asp.net generates thumbnail code

  • 2020-05-09 18:25:38
  • OfStack


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 class  
/// 1 Create thumbnail images or change the size and quality of the images to scale  
/// 2 , put the generated thumbnails into the specified directory  
/// </summary> 
public class ImageClass 
{ 
public System.Drawing.Image ResourceImage; 
private int ImageWidth; 
private int ImageHeight; 

public string ErrMessage; 

/// <summary> 
///  Class constructor  
/// </summary> 
/// <param name="ImageFileName"> The 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> 
///  Generate the thumbnail overload method 1 , return the thumbnail image Image object  
/// </summary> 
/// <param name="Width"> The width of the thumbnail </param> 
/// <param name="Height"> The height of the thumbnail </param> 
/// <returns> The thumbnail Image object </returns> 
public System.Drawing.Image GetReducedImage(int Width, int Height) 
{ 
try 
{ 
System.Drawing.Image ReducedImage; 

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 

ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero); 

return ReducedImage; 
} 
catch (Exception e) 
{ 
ErrMessage = e.Message; 
return null; 
} 
} 

/// <summary> 
///  Generate the thumbnail overload method 2 , saves the thumbnail file to the specified path  
/// </summary> 
/// <param name="Width"> The width of the thumbnail </param> 
/// <param name="Height"> The height of the thumbnail </param> 
/// <param name="targetFilePath"> The full file name of the thumbnail saved, ( With path ) , parameter format: D:Images ilename.jpg</param> 
/// <returns> Successfully returns true Otherwise return false</returns> 
public bool GetReducedImage(int Width, int Height, string targetFilePath) 
{ 
try 
{ 
System.Drawing.Image ReducedImage; 

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 

ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero); 
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg); 

ReducedImage.Dispose(); 

return true; 
} 
catch (Exception e) 
{ 
ErrMessage = e.Message; 
return false; 
} 
} 

/// <summary> 
///  Generate the thumbnail overload method 3 , return the thumbnail image Image object  
/// </summary> 
/// <param name="Percent"> The percentage of the width of the thumbnail   Percent is needed 80 , just fill in the 0.8</param> 
/// <returns> The thumbnail Image object </returns> 
public System.Drawing.Image GetReducedImage(double Percent) 
{ 
try 
{ 
System.Drawing.Image ReducedImage; 

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 

ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent); 
ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent); 

ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero); 

return ReducedImage; 
} 
catch (Exception e) 
{ 
ErrMessage = e.Message; 
return null; 
} 
} 

/// <summary> 
///  Generate the thumbnail overload method 4 , return the thumbnail image Image object  
/// </summary> 
/// <param name="Percent"> The percentage of the width of the thumbnail   Percent is needed 80 , just fill in the 0.8</param> 
/// <param name="targetFilePath"> The full file name of the thumbnail saved, ( With path ) , parameter format: D:Images ilename.jpg</param> 
/// <returns> Successfully returns true, Otherwise returns false</returns> 
public bool GetReducedImage(double Percent, string targetFilePath) 
{ 
try 
{ 
System.Drawing.Image ReducedImage; 

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 

ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent); 
ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent); 

ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero); 

ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg); 

ReducedImage.Dispose(); 

return true; 
} 
catch (Exception e) 
{ 
ErrMessage = e.Message; 
return false; 
} 
} 
} 

Related articles: