Common functions for generating high-quality thumbnails using ASP. NET (C # code)

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 (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalimage. Height * width/originalimage. width;
Break;
Case "H": // specify the height. The width is proportional.
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 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 and smooth Low Speed
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 the thumbnail in JPG format
Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );
}
Catch (system. Exception E)
{
Throw E;
}
Finally
{
Originalimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}

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.