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;
                }
            }
  • 相关阅读:
    nginx的简介和配置文件实例(一)
    Tomcat基础配置(一)
    redis主从复制以及SSDB主主复制环境部署记录(四)
    redis主从原理介绍(三)
    redis介绍和安装和主从介绍(二)
    celery
    网络-sdn
    djang问题汇总
    django路由url
    django开发环境配置
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/11065343.html
Copyright © 2011-2022 走看看