/// <Summary> /// generate a thumbnail /// </Summary> /// <Param name = "originalimagepath"> source image 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"> Create a thumbnail </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 the height/width Scaling (may be deformed) break; Case "W": // specify the width, high toheight = originalimage. height * width/originalimage. width; break; Case "H": // specify the height. The width is proportional to towidth = originalimage. width * Height/originalimage. height; break; Case "cut": // specify the height and width (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 bitmap = new system. drawing. bitmap (towidth, toheight); // create a drawing board graphics G = system. drawing. graphics. fromimage (Bitmap); // set the high quality interpolation method G. interpolationmode = system. drawing. drawing2d. interpolationmode. high; // set high quality, low speed rendering smoothness G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; // clear the canvas and fill g with a transparent background color. clear (color. transparent); // specify the position and size of the original image. drawimage (originalimage, new rectangle (0, 0, towidth, toheight), new rectangle (X, Y, ow, oh), graphicsunit. pixel); try {// Save the thumbnail bitmap in JPG format. save (thumbnailpath, system. drawing. imaging. imageformat. JPEG);} catch (system. exception e) {Throw E;} finally {originalimage. dispose (); bitmap. dispose (); G. dispose ();}}
Generate a thumbnail