Generate a thumbnail

Source: Internet
Author: User
Tags bmp image
/// <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

Related Article

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.