. Net

Source: Internet
Author: User

 

/// <Summary> /// generate a thumbnail /// </Summary> /// <Param name = "originalimagepath"> original image path </param> /// <param name = "thumbnailpath"> thumbnail path </param> // <Param name = "width"> thumbnail width </param> /// <Param name = "height"> thumbnail height </param> Public static void makethumbnail (string originalimagepath, string thumbnailpath, int width, int height) {// obtain the original image system. drawing. image originalimage = system. drawing. image. fromfile (originalimagepath); // the width and height of the thumbnail canvas int towidth = width; int toheight = height; // write the original image to the canvas coordinates and width and height (used to set the overflow part for cropping) int x = 0; int y = 0; int ow = originalimage. width; int Oh = originalimage. height; // original image canvas, set the coordinates and width (used to zoom in the overall width and height of the original image) int bg_x = 0; int bg_y = 0; int bg_w = towidth; int bg_h = toheight; // Multiple Variable double multiple = 0; // obtain the IF (originalimage. width> = originalimage. height) Multiple = (double) originalimage. width/(double) width; else multiple = (double) originalimage. height/(double) height; // the width and height of the uploaded image are smaller than the thumbnail if (ow <= width & OH <= height) {// based on the original width and height of the thumbnail bg_w = originalimage. width; bg_h = originalimage. height; // fill bg_x = convert with the background color in the blank part. toint32 (double) towidth-(double) OW)/2); bg_y = convert. toint32 (double) toheight-(double) OH)/2 );} // The width and height of the uploaded image are larger than those of the thumbnail else {// the width and height are scaled proportionally by bg_w = convert. toint32 (double) originalimage. width/multiple); bg_h = convert. toint32 (double) originalimage. height/multiple); // fill bg_y = convert with the background color in the blank part. toint32 (double) height-(double) bg_h)/2); bg_x = convert. toint32 (double) width-(double) bg_w)/2);} // create a BMP image and set the thumbnail size. system. drawing. image bitmap = new system. drawing. bitmap (towidth, toheight); // create a canvas system. drawing. graphics G = system. drawing. graphics. fromimage (Bitmap); // set the high quality interpolation method G. interpolationmode = system. drawing. drawing2d. interpolationmode. highqualitybilinear; // set high quality, low speed rendering smoothness G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; // clear the canvas and set the background color G. clear (system. drawing. colortranslator. fromhtml ("# f2f2f2"); // draw the specified part of the original image at the specified position and in the specified size // The first system. drawing. rectangle is the canvas coordinates and width and height of the original image. The second is the coordinates and width and height of the original image written on the canvas. The last parameter is to specify the value unit as pixel G. drawimage (originalimage, new system. drawing. rectangle (bg_x, bg_y, bg_w, bg_h), new system. drawing. rectangle (X, Y, ow, oh), system. drawing. graphicsunit. pixel); try {// obtain the image type string fileextension = system. io. path. getextension (originalimagepath ). tolower (); // saves the thumbnail based on the original image type. If the image is not in the original format, the image may be blurred or jagged. switch (fileextension) {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;} catch (system. exception e) {Throw E;} finally {originalimage. dispose (); bitmap. dispose (); G. dispose ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.