C # generate thumbnail image non-distorted method instance sharing _ Practical tips

Source: Internet
Author: User
Tags httpcontext

Copy Code code as follows:

<summary>
Get thumbnails
</summary>
<returns></returns>
public bool Getthumbimg ()
{
Try
{
String Imgpath; Original path
if (Imgsourceurl. IndexOf ("\", 0) <0)//using a relative path
{
Imgpath = HttpContext.Current.Server.MapPath (Imgsourceurl); Convert to 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)//is using a relative path
{
Thumbpath=httpcontext.current.server.mappath (Thumburl);//Convert to 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.