• C#压缩图片——高质量压缩方式


    传入Bitmap对象,以及新图片的长宽(Bitmap.Size),这样生成的就是跟原图尺寸一致的低质量图片

            public Bitmap GetImageThumb(Bitmap mg, Size newSize)
            {
                double ratio = 0d;
                double myThumbWidth = 0d;
                double myThumbHeight = 0d;
                int x = 0;
                int y = 0;
    
                Bitmap bp;
    
                if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
                Convert.ToDouble(newSize.Height)))
                    ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
                else
                    ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
                myThumbHeight = Math.Ceiling(mg.Height / ratio);
                myThumbWidth = Math.Ceiling(mg.Width / ratio);
    
                Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
                bp = new Bitmap(newSize.Width, newSize.Height);
                x = (newSize.Width - thumbSize.Width) / 2;
                y = (newSize.Height - thumbSize.Height);
                System.Drawing.Graphics g = Graphics.FromImage(bp);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
                g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);
    
                return bp;
            }
    View Code
  • 相关阅读:
    Qt 添加外部库文件
    实例属性的增删改查
    面向对象2 类属性的增删改查
    面向对象
    hashlib模块
    configparser模块
    logging模块
    re模块2
    计算器 暂时没解决小数问题
    re正则表达式
  • 原文地址:https://www.cnblogs.com/a7265813/p/3955411.html
Copyright © 2020-2023  润新知