Using System;
Using System.Collections.Generic;
Using System.Linq; Using System.Text;
Using System.Collections; Using System.Drawing;
Using System.IO;
Using System.Drawing.Drawing2D;
Using System.Drawing.Imaging;
Namespace Common {
<summary>//Picture processing class
</summary>
public class Imagehelper {
#region thumbnail image
<summary>
Generate thumbnail images
</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 aspect scaling (possibly deformed)
Break
Case "W"://Specify width, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://Specify high, wide proportionally
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "cut"://Specify aspect cut (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 image
Image bitmap = new Bitmap (towidth, toheight);
Create a new artboard
Graphics g = graphics.fromimage (bitmap);
Setting high-quality interpolation methods
G.interpolationmode = Interpolationmode.high;
Set high quality, low speed rendering smoothness
G.smoothingmode = smoothingmode.highquality;
Empty the canvas and fill it with a transparent background color
G.clear (System.Drawing.Color.Transparent);
Draws the specified portion of the original picture at the specified position and at the specified size
G.drawimage (Originalimage, New System.Drawing.Rectangle (0, 0, Towidth, toheight), New Rectangle (x, Y, Ow, OH), System.dra Wing. GraphicsUnit.Pixel);
try{
Save thumbnails in JPG format
Bitmap. Save (Thumbnailpath, imageformat.jpeg);
} catch (System.Exception e) {
Loghelper.writelog ("Crop picture error, original picture path:" + Originalimagepath + ", Save Picture Path:" + Thumbnailpath + ", exception", e);
Throw e;
} finally {
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}
}
#endregion
Thumbnail image generation