码迷,mamicode.com
首页 > 其他好文 > 详细

无损图片缩放

时间:2015-11-06 17:38:00      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

 1 public static void SaveThumbnail(Bitmap originBitmap, int width, int height, string filename)
 2         {
 3             using (var newImage = new Bitmap(width, height))
 4             {
 5                 using (var graphic = GetGraphic(originBitmap, newImage))
 6                 {
 7                     graphic.DrawImage(originBitmap, 0, 0, width, height);
 8 
 9                     
10                     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
11                     ImageCodecInfo ici = null;
12                     foreach (ImageCodecInfo codec in codecs)
13                     {
14                         if (codec.MimeType.IndexOf("png") > -1)
15                             ici = codec;
16                     }
17                     EncoderParameters encoderParams = new EncoderParameters();
18                     long[] qualityParam = new long[1];                    
19                     qualityParam[0] = 100;
20                     EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
21                     encoderParams.Param[0] = encoderParam;
22 
23                     newImage.Save(filename, ici, encoderParams);
24                 }
25             }
26         }
27 
28         private static Graphics GetGraphic(Image originImage, Bitmap newImage)
29         {
30             newImage.SetResolution(originImage.HorizontalResolution, originImage.VerticalResolution);
31             var graphic = Graphics.FromImage(newImage);
32             graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
33             graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
34             graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
35             graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
36             return graphic;
37         }

 

看了dudu的,但是调用报错,稍作修改

 

无损图片缩放

标签:

原文地址:http://www.cnblogs.com/gezifeiyang/p/4942981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!