Example:
Add a namespace as follows:
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
Using system. IO;
Bytes -----------------------------------------------------------------------------------------------
Display source image:
// Display the source image <br/> protected void btnimage_click (Object sender, eventargs e) <br/>{< br/> string filepath = server. mappath ("~ /Up/beauty .jpg "); <br/> // Method 1: <br/> // response. contenttype = "image/JPEG"; <br/> // response. writefile (filepath); </P> <p> // Method 2: <br/> filestream imagestr = new filestream (filepath, filemode. open); <br/> byte [] imagedata = new byte [imagestr. length]; <br/> imagestr. read (imagedata, 0, (INT) imagestr. length); <br/> response. outputstream. write (imagedata, 0, (INT) imagestr. length); </P> <p>}
Dimensions to be changed:
Private Static size newsize (INT maxwidth, int maxheight, int width, int height) <br/>{< br/> double W = 0.0; <br/> double H = 0.0; <br/> double Sw = convert. todouble (width); <br/> double SH = convert. todouble (height); <br/> double Mw = convert. todouble (maxwidth); <br/> double MH = convert. todouble (maxheight); </P> <p> If (SW <MW & SH <MH) // If maxwidth and maxheight are greater than the source image, the length and height of the thumbnail remain unchanged <br/>{< br/> W = Sw; <br/> H = sh; <br/>}< br/> else if (SW/SH)> (Mw/MH) <br/>{< br/> W = maxwidth; <br/> H = (w * Sh)/SW; <br/>}< br/> else <br/>{< br/> H = maxheight; <br/> W = (H * SW)/SH; <br/>}< br/> return new size (convert. toint32 (W), convert. toint32 (h); <br/>}
Convert to thumbnail:
// Convert to a thumbnail <br/> Public void sendsmallimage (string filename, string newfile, int maxheight, int maxwidth) <br/>{< br/> system. drawing. image IMG = system. drawing. image. fromfile (filename); // source Image Information <br/> system. drawing. imaging. imageformat thisformat = IMG. rawformat; // source image format </P> <p> size newsize = newsize (maxwidth, maxheight, IMG. width, IMG. height); // return the adjusted image width and height <br/> bitmap outbmp = new bitma P (newsize. width, newsize. height); <br/> graphics G = graphics. fromimage (outbmp); <br/> // sets the painting quality (high) of the canvas. <br/> G. compositingquality = compositingquality. highquality; <br/> G. smoothingmode = smoothingmode. highquality; <br/> G. interpolationmode = interpolationmode. highqualitybicubic; <br/> G. drawimage (IMG, new rectangle (0, 0, newsize. width, newsize. height), 0, 0, IMG. width, IMG. height, graphicsunit. PIX El); <br/> G. dispose (); <br/> // when the following <SPAN class = 'wp _ keywordlink'> Code </span> is used to save an image, set the Compression Quality <br/> encoderparameters encoderparams = new encoderparameters (); <br/> long [] Quality = new long [1]; <br/> quality [0] = 100; <br/> encoderparameter encoderparam = new encoderparameter (system. drawing. imaging. encoder. quality, quality); <br/> encoderparams. param [0] = encoderparam; <br/> // obtain the imagecod that contains information about the built-in image decoder. Ecinfo object. <Br/> imagecodecinfo [] arrayici = imagecodecinfo. getimageencoders (); <br/> imagecodecinfo effecici = NULL; <br/> for (INT x = 0; x <arrayici. length; X ++) <br/>{< br/> If (arrayici [X]. formatdescription. equals ("Jpeg") <br/>{< br/> policici = arrayici [X]; // sets JPEG encoding <br/> break; <br/>}< br/> If (already ici! = NULL) <br/>{< br/> outbmp. save (newfile, jpegici, encoderparams); <br/>}< br/> else <br/>{< br/> outbmp. save (newfile, thisformat); <br/>}< br/> IMG. dispose (); <br/> outbmp. dispose (); <br/>}
The page is a fileupload control. First, upload the source image and add an image control on the page. The width and height are set to 100 × 100 to compare the thumbnails.
Scale down the source image output to 100x100;