Generate thumbnails without distortion in C #

Source: Internet
Author: User

Find a document online ~ Share with you !!

Using the. NET method, the thumbnails generated by getthumbnailimage are severely distorted. We recommend that you generate a thumbnail without distortion.

/// <Summary>
/// Obtain the thumbnail
/// </Summary>
/// <Returns> </returns>
Public bool getthumbimg ()
{
Try
{
String imgpath; // original path
If (imgsourceurl. indexof ("\", 0) <0) // The relative path is used.
{
Imgpath = httpcontext. Current. server. mappath (imgsourceurl); // convert it to a physical path
}
Else
{
Imgpath = imgsourceurl;
}
System. Drawing. Image sourceimage = system. Drawing. image. fromfile (imgpath );
Int width = sourceimage. width;
Int Height = sourceimage. height;
If (thumbwidth <= 0)
{
Thumbwidth = 120;
}
If (thumbwidth> = width)
{
Return false;
}
Else
{
(Thumbwidth, thheight * thumbwidth/thwidth, null, intptr. Zero );
Image imgthumb = new system. Drawing. Bitmap (thumbwidth, height * thumbwidth/width );
System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (imgthumb );
G. interpolationmode = system. Drawing. drawing2d. interpolationmode. highqualitybicubic;
G. drawimage (sourceimage, new rectangle (0, 0, thumbwidth, height * thumbwidth/width), 0, 0, width, height, graphicsunit. pixel );
String thumbpath = "";
Sourceimage. Dispose ();
If (thumburl = "")
{
Thumbpath = imgpath;
}
If (thumbpath. indexof ("\", 0) <0) // The relative path is used.
{
Thumbpath = httpcontext. Current. server. mappath (thumburl); // convert it to a physical path
}
Imgthumb. Save (thumbpath, imageformat. JPEG );
Imgthumb. Dispose ();
Return true;
}
}
Catch
{
Throw;
}
}

 

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.