Copy Code code as follows:
Generate thumbnails
</summary>
<param name= "Originalimagepath" > Source map Path </param>
<param name= "Thumbnailpath" > Thumbnail path </param>
<param name= "width" > Thumbnail width </param>
<param name= "height" > thumbnail height </param>
<param name= "mode" > How to Generate thumbnails: HW Specifies width scaling (possibly morphing); W specifies a wide, high proportional H designation high, wide proportional cut specifies width cuts (not deformed) </param>
<param name= "mode" > the format to be saved by thumbnails (gif,jpg,bmp,png) is null or unknown types are considered jpg</param>
public static void Makethumbnail (String originalimagepath, string thumbnailpath, int width, int height, string mode, Strin G imagetype)
{
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
Switch (Imagetype.tolower ())
{
Case "GIF":
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Gif);
Break
Case "JPG":
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
Case "BMP":
Bitmap. Save (Thumbnailpath, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);
Break
Case "PNG":
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Png);
Break
Default
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
}
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}
}