c to generate image thumbnail class of 2 implementation ideas

  • 2020-06-12 08:49:37
  • OfStack

1 kind
 
/**//// <summary> 
///  Generate thumbnails  
/// </summary> 
/// <param name="originalImagePath"> Source map path (physical path) </param> 
/// <param name="thumbnailPath"> Thumbnail path (physical path) </param> 
/// <param name="width"> Thumbnail width </param> 
/// <param name="height"> Thumbnail height </param> 
/// <param name="mode"> How to generate thumbnails </param> 
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 
{ 
Image originalImage = Image.FromFile(originalImagePath); 
int towidth = width; 
int toheight = height; 
int x = 0; 
int y = 0; 
int ow = originalImage.Width; 
int oh = originalImage.Height; 
switch (mode) 
{ 
case "HW":// Specify height and width scaling (possible distortion)  
break; 
case "W":// Specify width and height to scale  
toheight = originalImage.Height * width/originalImage.Width; 
break; 
case "H":// Specify height and width in proportion  
towidth = originalImage.Width * height/originalImage.Height; 
break; 
case "Cut":// Specified height and width cut (without deformation)  
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight) 
{ 
oh = originalImage.Height; 
ow = originalImage.Height*towidth/toheight; 
y = 0; 
x = (originalImage.Width - ow)/2; 
} 
else 
{ 
ow = originalImage.Width; 
oh = originalImage.Width*height/towidth; 
x = 0; 
y = (originalImage.Height - oh)/2; 
} 
break; 
default : 
break; 
} 
// new 1 a bmp The picture  
Image bitmap = new System.Drawing.Bitmap(towidth,toheight); 
// new 1 A drawing board  
Graphics g = System.Drawing.Graphics.FromImage(bitmap); 
// Set up a high quality interpolation  
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
// Set high quality , Low speed presents smoothness  
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
// Empty the canvas and fill it with a transparent background color  
g.Clear(Color.Transparent); 
// Draws the specified part of the original image at the specified location and at the specified size  
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), 
new Rectangle(x, y, ow,oh), 
GraphicsUnit.Pixel); 
try 
{ 
// In order to jpg Format to save thumbnails  
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 
} 
catch(System.Exception e) 
{ 
throw e; 
} 
finally 
{ 
originalImage.Dispose(); 
bitmap.Dispose(); 
g.Dispose(); 
} 
} 

Key method Graphics. See DrawImage ms - help: / / MS NETFrameworkSDKv1. 1. CHS cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11 htm
2 kinds of
4 overloaded methods, some return Image object directly, some generate thumbnails, and save to the specified directory!
 
using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; 
/// <summary> 
///  Picture processing class  
/// 1 , generate thumbnail images or change the size and quality of images to scale  
/// 2 , put the generated thumbnails in the specified directory  
/// </summary> 
public class ImageClass 
{ 
public 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=Image.FromFile(ImageFileName); 
ErrMessage=""; 
} 
public bool ThumbnailCallback() 
{ 
return false; 
} 
/// <summary> 
///  Generate thumbnail overload methods 1 , which returns the thumbnail Image object  
/// </summary> 
/// <param name="Width"> The width of the thumbnail </param> 
/// <param name="Height"> Thumbnail height </param> 
/// <returns> The thumbnail Image object </returns> 
public Image GetReducedImage(int Width,int Height) 
{ 
try 
{ 
Image ReducedImage; 
Image.GetThumbnailImageAbort callb=new Image.GetThumbnailImageAbort(ThumbnailCallback); 
ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero); 
return ReducedImage; 
} 
catch(Exception e) 
{ 
ErrMessage=e.Message; 
return null; 
} 
} 
/// <summary> 
///  Generate thumbnail overload methods 2 , saves the thumbnail file to the specified path  
/// </summary> 
/// <param name="Width"> The width of the thumbnail </param> 
/// <param name="Height"> Thumbnail height </param> 
/// <param name="targetFilePath"> Full filename saved by thumbnail, ( 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 
{ 
Image ReducedImage; 
Image.GetThumbnailImageAbort callb=new 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 thumbnail overload methods 3 , which returns the thumbnail Image object  
/// </summary> 
/// <param name="Percent"> Percentage width of thumbnails   Percent is needed 80 , just fill in the 0.8</param> 
/// <returns> The thumbnail Image object </returns> 
public Image GetReducedImage(double Percent) 
{ 
try 
{ 
Image ReducedImage; 
Image.GetThumbnailImageAbort callb=new 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 thumbnail overload methods 4 , which returns the thumbnail Image object  
/// </summary> 
/// <param name="Percent"> Percentage width of thumbnails   Percent is needed 80 , just fill in the 0.8</param> 
/// <param name="targetFilePath"> Full filename saved by thumbnail, ( 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 
{ 
Image ReducedImage; 
Image.GetThumbnailImageAbort callb=new 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: