• 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.         }  
  • 相关阅读:
    GWT中如何将EMF对象存储为XMI/从XMI反序列化为对象
    springboot工程启动时报JSR330 'javax.inject.Inject' annotation found and supported for autowiring ,启动缓慢甚至卡住不动
    深度剖析ArrayList和LinkedList
    IRasterGeometryProc,一个可以控制raster对象的接口
    sessionStorage的使用
    spring配置问题:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors...java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are
    Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if exp
    No 'AccessControlAllowOrigin' header is present之跨域问题及解决方案
    Caused by: java.lang.NoClassDefFoundError: Ljavax/servlet/http/HttpServletRequest;
  • 原文地址:https://www.cnblogs.com/Alex80/p/5127123.html
Copyright © 2020-2023  润新知