zoukankan      html  css  js  c++  java
  • C# 缩小图片尺寸

    public static Image ReduceImage(string imageFile, int toWidth, int toHeight)
    {
        Image originalImage = GetImage(imageFile);
        if (toWidth <= 0 && toHeight <= 0)
        {
            return originalImage;
        }
        else if (toWidth > 0 && toHeight > 0)
        {
            if (originalImage.Width < toWidth && originalImage.Height < toHeight)
                return originalImage;
        }
        else if (toWidth <= 0 && toHeight > 0)
        {
            if (originalImage.Height < toHeight)
                return originalImage;
            toWidth = originalImage.Width * toHeight / originalImage.Height;
        }
        else if (toHeight <= 0 && toWidth > 0)
        {
            if (originalImage.Width < toWidth)
                return originalImage;
            toHeight = originalImage.Height * toWidth / originalImage.Width;
        }
        Image toBitmap = new Bitmap(toWidth, toHeight);
        using (Graphics g = Graphics.FromImage(toBitmap))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage,
                        new Rectangle(00, toWidth, toHeight),
                        new Rectangle(00, originalImage.Width, originalImage.Height),
                        GraphicsUnit.Pixel);
            originalImage.Dispose();
            return toBitmap;
        }
    }
  • 相关阅读:
    【新阁教育】爱普生机器人建立工具坐标系教程
    BPF CO-RE 示例代码解析
    gRPC Load Balancing
    Linux Clone函数
    高性能 Nginx HTTPS 调优
    内网渗透测试:内网横向移动基础总结
    Python 运算符
    华为云-容器引擎CCE-部署Nginx应用
    华为云-云容器引擎(CCE)-高危操作及解决方案
    周杰伦新歌《说好不哭》上线,程序员哭了......
  • 原文地址:https://www.cnblogs.com/anjou/p/2575907.html
Copyright © 2011-2022 走看看