C # Methods for generating thumbnail images without distortion

Source: Internet
Author: User

  using the. Net method to getthumbnailimage the resulting thumbnail is highly distorted, and here is a recommended method for generating thumbnails without distortion

Code as follows:///&lt;summary&gt;///get thumbnail///&lt;/summary&gt;///&lt;returns&gt;&lt;/returns&gt; &nbsp; public bool Getthumbi MG () {try {string Imgpath;//original path &nbsp; &nbsp; &nbsp;if (Imgsourceurl. IndexOf ("", 0) &lt;0)//using the relative path &nbsp; &nbsp; &nbsp;{Imgpath = HttpContext.Current.Server.MapPath (Imgsourceurl); Convert to physical path &nbsp; &nbsp; &NBSP;} else {imgpath=imgsourceurl;} System.Drawing.Image sourceimage = System.Drawing.Image.FromFile (Imgpath); int width = sourceimage.width; int height = sourceimage.height; if (thumbwidth &lt;= 0) {thumbwidth = <;} if (Thumbwidth &gt;= width) {return false;} else {(Thumbwidth,thheight*thum Bwidth/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) &lt;0)//is used relative path &nbsp; &nbsp; &nbsp; {thumbpath=httpcontext.current.server.mappath (thumburl); Convert to physical path &nbsp; &nbsp; &nbsp;} imgthumb.save (Thumbpath,imageformat.jpeg); Imgthumb.dispose (); return true; The catch {Throw}}} &nbsp;

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.