zoukankan      html  css  js  c++  java
  • c# 无损高质量压缩图片代码

    1. /// <summary>  
    2.         /// 无损压缩图片  
    3.         /// </summary>  
    4.         /// <param name="sFile">原图片</param>  
    5.         /// <param name="dFile">压缩后保存位置</param>  
    6.         /// <param name="dHeight">高度</param>  
    7.         /// <param name="dWidth"></param>  
    8.         /// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>  
    9.         /// <returns></returns>  
    10.         public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)  
    11.         {  
    12.   
    13.             System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);  
    14.             ImageFormat tFormat = iSource.RawFormat;  
    15.             int sW = 0, sH = 0;  
    16.             //按比例缩放  
    17.             Size tem_size = new Size(iSource.Width, iSource.Height);  
    18.   
    19.             if (tem_size.Width > dHeight || tem_size.Width > dWidth) //将**改成c#中的或者操作符号  
    20.             {  
    21.                 if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))  
    22.                 {  
    23.                     sW = dWidth;  
    24.                     sH = (dWidth * tem_size.Height) / tem_size.Width;  
    25.                 }  
    26.                 else  
    27.                 {  
    28.                     sH = dHeight;  
    29.                     sW = (tem_size.Width * dHeight) / tem_size.Height;  
    30.                 }  
    31.             }  
    32.             else  
    33.             {  
    34.                 sW = tem_size.Width;  
    35.                 sH = tem_size.Height;  
    36.             }  
    37.             Bitmap ob = new Bitmap(dWidth, dHeight);  
    38.             Graphics g = Graphics.FromImage(ob);  
    39.             g.Clear(Color.WhiteSmoke);  
    40.             g.CompositingQuality = CompositingQuality.HighQuality;  
    41.             g.SmoothingMode = SmoothingMode.HighQuality;  
    42.             g.InterpolationMode = InterpolationMode.HighQualityBicubic;  
    43.             g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);  
    44.             g.Dispose();  
    45.             //以下代码为保存图片时,设置压缩质量  
    46.             EncoderParameters ep = new EncoderParameters();  
    47.             long[] qy = new long[1];  
    48.             qy[0] = flag;//设置压缩的比例1-100  
    49.             EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);  
    50.             ep.Param[0] = eParam;  
    51.             try  
    52.             {  
    53.                 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();  
    54.                 ImageCodecInfo jpegICIinfo = null;  
    55.                 for (int x = 0; x < arrayICI.Length; x++)  
    56.                 {  
    57.                     if (arrayICI[x].FormatDescription.Equals("JPEG"))  
    58.                     {  
    59.                         jpegICIinfo = arrayICI[x];  
    60.                         break;  
    61.                     }  
    62.                 }  
    63.                 if (jpegICIinfo != null)  
    64.                 {  
    65.                     ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径  
    66.                 }  
    67.                 else  
    68.                 {  
    69.                     ob.Save(dFile, tFormat);  
    70.                 }  
    71.                 return true;  
    72.             }  
    73.             catch  
    74.             {  
    75.                 return false;  
    76.             }  
    77.             finally  
    78.             {  
    79.                 iSource.Dispose();  
    80.                 ob.Dispose();  
    81.             }  
    82.   
    83.         }  
  • 相关阅读:
    android 中文 api (43) —— Chronometer
    SVN客户端清除密码
    Android 中文 API (35) —— ImageSwitcher
    Android 中文API (46) —— SimpleAdapter
    Android 中文 API (28) —— CheckedTextView
    Android 中文 API (36) —— Toast
    Android 中文 API (29) —— CompoundButton
    android 中文 API (41) —— RatingBar.OnRatingBarChangeListener
    Android 中文 API (30) —— CompoundButton.OnCheckedChangeListener
    Android 中文 API (24) —— MultiAutoCompleteTextView.CommaTokenizer
  • 原文地址:https://www.cnblogs.com/Alex80/p/5127123.html
Copyright © 2011-2022 走看看