[C #] generating thumbnails

Source: Internet
Author: User

/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "sourceimagepath"> original image path (absolute path) </param>
/// <Param name = "thumbnailimagewidth"> width of the thumbnail (the height is automatically generated based on the proportion of the source image) </param>
Public Image tothumbnailimages (string sourceimagepath, int thumbnailimagewidth)
{
Image retimage = NULL;

String sourceimagepath = sourceimagepath;
Int thumbnailimagewidth = thumbnailimagewidth;
String sext = sourceimagepath. substring (sourceimagepath. lastindexof ("."). tolower ();
// Create an image object from the original image
Stream FS = new filestream (sourceimagepath, filemode. Open );
System. Drawing. Image image = system. Drawing. image. fromstream (FS );
FS. Close ();
// System. Drawing. Image image = system. Drawing. image. fromfile (sourceimagepath );
Int num = (thumbnailimagewidth/4) * 3 );
Int width = image. width;
Int Height = image. height;
// Calculate the image proportion
If (double) width)/(double) height)> = 1.33333333333333f)
{
Num = (height * thumbnailimagewidth)/width );
}
Else
{
Thumbnailimagewidth = (width * num)/height );
}
If (thumbnailimagewidth <1) | (Num <1 ))
{
Return retimage;
}
// Initialize a new bitmap instance with the specified size and format
Bitmap bitmap = new Bitmap (thumbnailimagewidth, num, pixelformat. format32bppargb );
// Create a new graphics object from the specified image object
Graphics graphics = graphics. fromimage (Bitmap );
// Clear the entire drawing surface and fill it with a transparent background color
Graphics. Clear (color. Transparent );
// Draw the original image object at the specified position and by the specified size
Graphics. drawimage (image, new rectangle (0, 0, thumbnailimagewidth, num ));
Retimage = image. Clone () as image;
Image. Dispose ();
Bitmap. Dispose ();
Graphics. Dispose ();

Return retimage;
}

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.