The following Code generates image thumbnails in four modes based on the width and height of the generated image.
Using system; using system. collections; using system. configuration; using system. data; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. io; public partial class imgoperator_imagecut: system. web. UI. page {protected void page_load (Object sender, eventargs e) {makethumbimag E ("~ /Images/dsc01010.jpg ","~ /Images/cut5.jpg ", 800,450," bgcut ");} private void makethumbimage (string initpath, string outpath, int width, int height, string mode) {system. drawing. image image = system. drawing. image. fromfile (server. mappath (initpath); int Tw = width; int th = height; // the size of the generated image. Int Sw = image. width; int SH = image. height; int x = 0; int y = 0; Switch (mode) {Case "HW": break; Case "W": Th = width * image. height/image. width; break; Case "H": Tw = height * image. width/image. height; break; Case "cut": If (double) SW/(double) Sh <(double) width/(double) height) {Sw = image. width; SH = image. width * Th/TW; X = 0; y = (image. height-SH)/2;} else {SH = image. height; Sw = image. height * TW/TH; X = (image. width-SW)/2; y = 0;} break; Case "bgcut": If (double) width/(double) height <(double) image. width/(double) image. height) {Sw = image. width; SH = image. width * Th/TW; X = 0; y = (image. height-SH)/2;} else {SH = image. height; Sw = image. height * TW/TH; X = (image. width-SW)/2; y = 0;} break; default: break;} system. drawing. bitmap BMP = new system. drawing. bitmap (TW, th); system. drawing. graphics G = system. drawing. graphics. fromimage (BMP); G. interpolationmode = system. drawing. drawing2d. interpolationmode. high; G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; G. clear (system. drawing. color. whitesmoke); G. drawimage (image, new system. drawing. rectangle (0, 0, TW, th), new system. drawing. rectangle (X, Y, SW, SH), system. drawing. graphicsunit. pixel); try {string opath = server. mappath (outpath); BMP. save (opath, system. drawing. imaging. imageformat. JPEG); filestream FS = new filestream (opath, filemode. open, fileaccess. read); byte [] imgdata = new byte [(INT) FS. length]; FS. read (imgdata, 0, (INT) FS. length); response. binarywrite (imgdata); response. contenttype = "image/pjpeg"; response. end (); // BMP. save (response. outputstream, system. drawing. imaging. imageformat. JPEG);} catch (exception ex) {response. write (ex. message);} finally {image. dispose (); BMP. dispose (); G. dispose ();}}}