• 图片缩放


    /// <summary>
            /// 按比例缩放,图片不会变形,会优先满足原图和最大长宽比例最高的一项
            /// </summary>
            /// <param name="oldPath"></param>
            /// <param name="newPath"></param>
            /// <param name="maxWidth"></param>
            /// <param name="maxHeight"></param>
            public static void CompressPercent(string oldPath, string newPath, int maxWidth, int maxHeight)
            {
                Image _sourceImg = Image.FromFile(oldPath);
                double _newW = (double)maxWidth;
                double _newH = (double)maxHeight;
                double percentWidth = (double)_sourceImg.Width > maxWidth ? (double)maxWidth : (double)_sourceImg.Width;
    
                if ((double)_sourceImg.Height * (double)percentWidth / (double)_sourceImg.Width > (double)maxHeight)
                {
                    _newH = (double)maxHeight;
                    _newW = (double)maxHeight / (double)_sourceImg.Height * (double)_sourceImg.Width;
                }
                else
                {
                    _newW = percentWidth;
                    _newH = (percentWidth / (double)_sourceImg.Width) * (double)_sourceImg.Height;
                }
                Image bitmap = new Bitmap((int)_newW, (int)_newH);
                Graphics g = Graphics.FromImage(bitmap);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(Color.Transparent);
                g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
                _sourceImg.Dispose();
                g.Dispose();
                bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                bitmap.Dispose();
            }
    
            /// <summary>
            /// 按照指定大小对图片进行缩放,可能会图片变形
            /// </summary>
            /// <param name="oldPath"></param>
            /// <param name="newPath"></param>
            /// <param name="newWidth"></param>
            /// <param name="newHeight"></param>
            public static void ImageChangeBySize(string oldPath, string newPath, int newWidth, int newHeight)
            {
                Image sourceImg = Image.FromFile(oldPath);
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight);
                Graphics g = Graphics.FromImage(bitmap);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(Color.Transparent);
                g.DrawImage(sourceImg, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), GraphicsUnit.Pixel);
                sourceImg.Dispose();
                g.Dispose();
                bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                bitmap.Dispose();
            }
  • 相关阅读:
    项目使用 GlobalExceptionHandler 与 @RestControllerAdvice自定义异常 二
    spring,springBoot配置类型转化器Converter以及FastJsonHttpMessageConverter,StringHttpMessageConverter 使用
    项目使用 GlobalExceptionHandler 自定义异常 一
    idea 解决git更新冲突
    @JsonIgnore 失效没起作用及 @JSONField(serialize = false)
    Don't Sleep --- 阻止电脑休眠、睡眠小工具
    Win10 一键启用&禁用以太网bat命令
    如何将Chrome插件扩展下载到本地
    PC WorkBreak --- 在您使用 PC 时照顾您的健康工具
    闪电下载器
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/12018081.html
Copyright © 2020-2023  润新知