Asp.net common function for generating thumbnails (Multiple Generation modes are supported)

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"> How to generate 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 high-width Scaling (may be deformed)
Break ;
Case   " W " : // Specify width and height in proportion
Toheight = Originalimage. Height * Width / Originalimage. width;
Break ;
Case   " H " : // Specify height, width by proportion
Towidth = Originalimage. Width * Height / Originalimage. height;
Break ;
Case   " Cut " : // Specify height and width reduction (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 BMP Image
Image bitmap =   New System. Drawing. Bitmap (towidth, toheight );

// Create a canvas
Graphics g = System. Drawing. Graphics. fromimage (Bitmap );

// Set a 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 it with a transparent background color
G. Clear (color. Transparent );

// Draw the specified part of the original image at the specified position and in the specified size
G. drawimage (originalimage, New Rectangle ( 0 , 0 , Towidth, toheight ),
New Rectangle (X, Y, ow, oh ),
Graphicsunit. pixel );

Try
{
//Save thumbnails in JPG format
Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );
}
Catch (System. Exception E)
{
ThrowE;
}
Finally
{
Originalimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}

For the key method graphics. drawimage, see
MS-help: // Ms. netframeworksdkv1.1.chs/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm

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.