First Kind
Copy Code code as follows:
/**////<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 width scaling (possibly distorted)
Break
Case "W"://Specify wide, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://specified high, wide proportional
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "cut"://Specify width trim (not deformed)
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
}
Create a new BMP picture
Image bitmap = new System.Drawing.Bitmap (towidth,toheight);
Create a new artboard
Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Set high quality interpolation method
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Empty the canvas and fill with a transparent background color
G.clear (color.transparent);
Draws the specified portion of the original picture 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
{
Save thumbnails in JPG format
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}
}
Key methods Graphics.DrawImage See ms-help://ms.netframeworksdkv1.1.chs/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm
Second Kind
4 overloaded methods that have a direct return image object, have the build thumbnail, and save to the specified directory!
Copy Code code as follows:
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 pictures proportionally
2. Place the generated thumbnail in the specified directory
</summary>
public class Imageclass
{
Public Image Resourceimage;
private int imagewidth;
private int imageheight;
public string errmessage;
<summary>
Constructors for classes
</summary>
<param name= "Imagefilename" > Picture file full path name </param>
Public Imageclass (String imagefilename)
{
Resourceimage=image.fromfile (Imagefilename);
Errmessage= "";
}
public bool Thumbnailcallback ()
{
return false;
}
<summary>
Generate thumbnail overload Method 1, returning the image object of the thumbnail
</summary>
<param name= "width" > thumbnail widths </param>
<param name= "height" > Thumbnail Heights </param>
<returns> 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 Method 2, save the thumbnail file to the specified path
</summary>
<param name= "width" > thumbnail widths </param>
<param name= "height" > Thumbnail Heights </param>
<param name= "Targetfilepath" > Thumbnail saved full-text name, (with path), parameter format: D:images ilename.jpg</param>
<returns> return true successfully, 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 Method 3, returning the image object of the thumbnail
</summary>
<param name= "Percent" > thumbnail width of the percentage such as: Required 80, to fill 0.8</param>
<returns> 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 method 4, returning the image object of the thumbnail
</summary>
<param name= "Percent" > thumbnail width of the percentage such as: Required 80, to fill 0.8</param>
<param name= "Targetfilepath" > Thumbnail saved full-text name, (with path), parameter format: D:images ilename.jpg</param>
<returns> return true successfully, otherwise return 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;
}
}
}