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.         }  
  • 相关阅读:
    今天学习的数组不是很懂
    今天的复习内容
    这是今天的作业
    总结,错误反思
    php 文件读取
    call_user_func函数
    plsql中调试函数 转
    oracle数据库中函数的递归调用
    关闭PdfReader右侧工具栏的方法
    Dev TreeList基本用法
  • 原文地址:https://www.cnblogs.com/Alex80/p/5127123.html
Copyright © 2011-2022 走看看