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