zoukankan      html  css  js  c++  java
  • C# 无损压缩图片

    View Code
     1 //命名空间
     2 using System.Drawing.Imaging;
     3 using System.Drawing;
     4 using System.Drawing.Drawing2D;
     5 
     6    /// <summary>
     7         /// 无损压缩图片
     8         /// </summary>
     9         /// <param name="sFile">原图片</param>
    10         /// <param name="dFile">压缩后保存位置</param>
    11         /// <param name="dHeight">高度</param>
    12         /// <param name="dWidth"></param>
    13         /// <param name="flag">压缩质量 1-100</param>
    14         /// <returns></returns>
    15         public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
    16         {
    17 
    18             System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
    19             ImageFormat tFormat = iSource.RawFormat;
    20             int sW = 0, sH = 0;
    21             //按比例缩放
    22             Size tem_size = new Size(iSource.Width, iSource.Height);
    23 
    24             if (tem_size.Width > dHeight||tem_size.Width > dWidth) 
    25             {
    26                 if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
    27                 {
    28                     sW = dWidth;
    29                     sH = (dWidth * tem_size.Height) / tem_size.Width;
    30                 }
    31                 else
    32                 {
    33                     sH = dHeight;
    34                     sW = (tem_size.Width * dHeight) / tem_size.Height;
    35                 }
    36             }
    37             else
    38             {
    39                 sW = tem_size.Width;
    40                 sH = tem_size.Height;
    41             }
    42             Bitmap ob = new Bitmap(dWidth, dHeight);
    43             Graphics g = Graphics.FromImage(ob);
    44             g.Clear(Color.WhiteSmoke);
    45             g.CompositingQuality = CompositingQuality.HighQuality;
    46             g.SmoothingMode = SmoothingMode.HighQuality;
    47             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    48             g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
    49             g.Dispose();
    50             //以下代码为保存图片时,设置压缩质量
    51             EncoderParameters ep = new EncoderParameters();
    52             long[] qy = new long[1];
    53             qy[0] = flag;//设置压缩的比例1-100
    54             EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
    55             ep.Param[0] = eParam;
    56             try
    57             {
    58                 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
    59                 ImageCodecInfo jpegICIinfo = null;
    60                 for (int x = 0; x < arrayICI.Length; x++)
    61                 {
    62                     if (arrayICI[x].FormatDescription.Equals("JPEG"))
    63                     {
    64                         jpegICIinfo = arrayICI[x];
    65                         break;
    66                     }
    67                 }
    68                 if (jpegICIinfo != null)
    69                 {
    70                     ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
    71                 }
    72                 else
    73                 {
    74                     ob.Save(dFile, tFormat);
    75                 }
    76                 return true;
    77             }
    78             catch
    79             {
    80                 return false;
    81             }
    82             finally
    83             {
    84                 iSource.Dispose();
    85                 ob.Dispose();
    86             }
    87 
    88         }

    来至网摘

  • 相关阅读:
    Ubuntu: ImportError: No module named xgboost
    Sample a balance dataset from imbalance dataset and save it(从不平衡数据中抽取平衡数据,并保存)
    Python 对不均衡数据进行Over sample(重抽样)
    汪培庄先生的个人主页
    因素空间, 人工智能的新数学理论
    百度在线笔试编程测试题(Python):整数分解成素数的积
    Android.mk 文件语法详解 转:http://blog.sina.com.cn/s/blog_602f8770010148ce.html
    Git入门指南十一:Git branch 分支与合并分支
    List of devices attached ???????????? no permissions
    Git常用命令
  • 原文地址:https://www.cnblogs.com/zzcong/p/2497180.html
Copyright © 2011-2022 走看看