[Go] [C #] Resolve generated thumbnail blur issues

Source: Internet
Author: User

First, the problem scenario

Generates thumbnails of 48pxx48px for a square picture and saves them as picture files, but finds that the generated thumbnails are blurry.

The resulting blurred thumbnails are as follows:

Original picture (300pxx300px, PNG format):

System.Drawing.Image is called in the code. Getthumbnailimage () method, the main implementation code is as follows:

1 Private voidSavethumbnail (Bitmap Bitmap,intWidthintHeightstringDirectorystringFileNamestringextension)2 {3     varPhysicalPath = directory + filename +extension;4     using(varthumbnail = bitmap. Getthumbnailimage (width, height, () = {return true;}, IntPtr.Zero))5     {6         using(varEncoderParameters =NewEncoderParameters (1))7         {8encoderparameters.param[0] =NewEncoderparameter (Encoder.quality,100L);9 thumbnail. Save (PhysicalPath,Ten imagecodecinfo.getimageencoders () One. Where (x =x.filenameextension.contains (extension. Toupperinvariant ())) A                             . FirstOrDefault (), - encoderparameters); -         } the     }            -}

Second, the solution

Instead, call the System.Drawing.Graphics.DrawImage () method, and the main implementation code is as follows:

1 Private voidSavethumbnail (Bitmap Originbitmap,intWidthintHeightstringDirectorystringFileNamestringextension)2 {3     varPhysicalPath = directory + filename +extension;4             5     using(varNewImage =NewBitmap (width, height))6     {7         using(varGraphic =getgraphic (Originbitmap, newimage))8         {9Graphic. DrawImage (Originbitmap,0,0, width, height);Ten             using(varEncoderParameters =NewEncoderParameters (1)) One             { Aencoderparameters.param[0] =NewEncoderparameter (Encoder.quality,100L); - Newimage.save (PhysicalPath, - imagecodecinfo.getimageencoders () the. Where (x =x.filenameextension.contains (extension. Toupperinvariant ())) -                                 . FirstOrDefault (), - encoderparameters); -             } +         } -     }             + } A  at PrivateGraphics getgraphic (Image originimage, Bitmap newimage) - { - newimage.setresolution (originimage.horizontalresolution, originimage.verticalresolution); -     varGraphic =graphics.fromimage (newimage); -Graphic. Interpolationmode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; -Graphic. SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; inGraphic. Pixeloffsetmode =System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; -Graphic.compositingquality =System.Drawing.Drawing2D.CompositingQuality.HighQuality; to     returngraphic; +}

The resulting thumbnail effect is as follows:

Iii. references

Cropping image using JQuery, Jcrop and ASP

Resizing an Image without losing any quality

[Go] [C #] Resolve generated thumbnail blur issues

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.