zoukankan      html  css  js  c++  java
  • 图片压缩

            /// <summary>
            /// 将图片按百分比压缩
            /// </summary>
            public static bool ImageCompress(Image source, string savePath, byte percent)
            {
                var parameters = new EncoderParameters();
                parameters.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { percent });
                try
                {
                    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
                    ImageCodecInfo jpegICIinfo = arrayICI.SingleOrDefault(a => a.FormatDescription.Equals("JPEG", StringComparison.OrdinalIgnoreCase));
                    if (jpegICIinfo != null)
                        source.Save(savePath, jpegICIinfo, parameters);
                    else
                        source.Save(savePath, source.RawFormat);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    source.Dispose();
                }
            }
            public static Bitmap PercentImage(Image srcImage)
            {
                int newW = srcImage.Width < 1130 ? srcImage.Width : 1130;
                int newH = int.Parse(Math.Round(srcImage.Height * (double)newW / srcImage.Width).ToString());
                try
                {
                    Bitmap b = new Bitmap(newW, newH);
                    Graphics g = Graphics.FromImage(b);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
                    g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
                    g.Dispose();
                    return b;
                }
                catch (Exception)
                {
                    return null;
                }
            }
  • 相关阅读:
    bzoj1996
    bzoj2839
    bzoj1304
    bzoj1097
    bzoj4547
    bzoj3379
    bzoj3090
    树莓派/Debian 构建LAMP Web服务器并搭建WordPress博客(一)
    树莓派/Debian Apache2 配置自建 CA 实现 HTTPS(SSL) 服务
    树莓派/Debian Apache2 实现 HTTPS(SSL) 服务
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/11065343.html
Copyright © 2011-2022 走看看