C#图片按比例缩放的实现代码

时间:2022-12-19 03:58:16 


using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Publics
{
    public class ImgHelper
    {
        public static void AdjustPhoto(int toWidth, int toHeight, string filePath, string fromFileName, string toFileName, int maxWidth, int maxHeight)
        {
            Image originalImage = Image.FromFile(filePath + "/" + fromFileName);
            //如果尺寸不够返回保存原图
            if (originalImage.Width < toWidth && originalImage.Height < toHeight)
            {
                originalImage.Save(filePath + "/" + toFileName);
                originalImage.Dispose();
                return;
            }

            //根据图片大小获取新图片从原图片截取的区域
            int x, y, w, h;
            if (toHeight > 0)
            {
                if (toWidth > 0)
                {
                    if (originalImage.Width > toWidth && originalImage.Height > toHeight)
                    {
                        w = toWidth;
                        h = toWidth * originalImage.Height / originalImage.Width;

                        if (h > toHeight)
                        {
                            h = toHeight;
                            w = toHeight * originalImage.Width / originalImage.Height;
                            x = (toWidth - w) / 2;
                            y = 0;
                        }
                        else
                        {
                            x = 0;
                            y = (toHeight - h) / 2;
                        }
                    }
                    else if (originalImage.Width > toWidth)
                    {
                        w = toWidth;
                        h = toWidth * originalImage.Height / originalImage.Width;
                        x = 0;
                        y = (toHeight - h) / 2;
                    }
                    else if (originalImage.Height > toHeight)
                    {
                        h = toHeight;
                        w = toHeight * originalImage.Width / originalImage.Height;
                        x = (toWidth - w) / 2;
                        y = 0;
                    }
                    else
                    {
                        w = originalImage.Width;
                        h = originalImage.Height;
                        x = (toWidth - w) / 2;
                        y = (toHeight - h) / 2;
                    }
                }
                else
                {
                    if (originalImage.Height > maxHeight)
                    {
                        toWidth = toHeight * originalImage.Width / originalImage.Height;
                        x = 0;
                        y = 0;
                        w = toWidth;
                        h = toHeight;

                    }
                    else
                    {
                        x = 0;
                        y = 0;
                        w = originalImage.Width;
                        h = originalImage.Height;
                        toWidth = originalImage.Width;
                        toHeight = originalImage.Height;
                    }
                }
            }
            else
            {
                if (originalImage.Width > maxWidth)
                {
                    toHeight = toWidth * originalImage.Height / originalImage.Width;
                    x = 0;
                    y = 0;
                    w = toWidth;
                    h = toHeight;

                }
                else
                {
                    x = 0;
                    y = 0;
                    w = originalImage.Width;
                    h = originalImage.Height;
                    toWidth = originalImage.Width;
                    toHeight = originalImage.Height;
                }
            }
            Bitmap bm = new Bitmap(toWidth, toHeight);
            Graphics g = Graphics.FromImage(bm);

            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.Clear(Color.White);
            g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);

            long[] quality = new long[1];
            quality[0] = 80;

            EncoderParameters encoderParams = new EncoderParameters();
            EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
            ImageCodecInfo jpegICI = null;
            for (int i = 0; i < arrayICI.Length; i++)
            {
                if (arrayICI[i].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[i];//设置JPEG编码
                    break;
                }
            }
            if (jpegICI != null)
            {

                //bm.Save(Server.MapPath(path + "/thumb_" + filename), jpegICI, encoderParams);
                bm.Save(filePath + "/" + toFileName, jpegICI, encoderParams);
            }

            bm.Dispose();
            originalImage.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 保持比例图像缩放简易算法
        /// </summary>
        /// <param name="spcWidth"></param>
        /// <param name="spcHeight"></param>
        /// <param name="orgWidth"></param>
        /// <param name="orgHeight"></param>
        /// <returns></returns>
        public static Dictionary<string, int> AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
        {
            Dictionary<string, int> size = new Dictionary<string, int>();
            // 原始宽高在指定宽高范围内,不作任何处理 
            if (orgWidth <= spcWidth && orgHeight <= spcHeight)
            {
                size["Width"] = orgWidth;
                size["Height"] = orgHeight;
            }
            else
            {
                // 取得比例系数 
                float w = orgWidth / (float)spcWidth;
                float h = orgHeight / (float)spcHeight;
                // 宽度比大于高度比 
                if (w > h)
                {
                    size["Width"] = spcWidth;
                    size["Height"] = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
                }
                // 宽度比小于高度比 
                else if (w < h)
                {
                    size["Height"] = spcHeight;
                    size["Width"] = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
                }
                // 宽度比等于高度比 
                else
                {
                    size["Width"] = spcWidth;
                    size["Height"] = spcHeight;
                }
            }
            return size;
        }
    }
}

标签:图片缩放,C#
0
投稿

猜你喜欢

  • dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter

    2023-11-24 14:25:13
  • java字符串抉择

    2023-08-08 14:20:05
  • 详解Java如何实现在PDF中插入,替换或删除图像

    2022-04-07 22:40:36
  • Java 日志打印的15个好建议

    2021-09-08 17:57:42
  • java 数组越界判断和获取数组长度的实现方式

    2023-05-24 07:06:52
  • Android 连接Wifi和创建Wifi热点的实例

    2022-07-16 06:00:52
  • Android实战教程第一篇之最简单的计算器

    2023-03-12 17:57:54
  • Android仿支付宝微信支付密码界面弹窗封装dialog

    2021-10-24 13:13:31
  • WinFrom中label背景透明的实现方法

    2023-05-08 13:05:40
  • Java byte数组操纵方式代码实例解析

    2022-02-18 16:54:12
  • SpringMvc框架的简介与执行流程详解

    2022-10-15 18:49:00
  • SpringBoot AOP控制Redis自动缓存和更新的示例

    2023-08-31 17:34:37
  • Java实现按权重随机数

    2023-11-28 23:15:32
  • android底层去掉虚拟按键的实例讲解

    2022-01-29 17:53:14
  • JavaFX实现简单日历效果

    2023-05-16 08:43:30
  • spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

    2022-01-04 20:40:56
  • C++ 前置声明详解及实例

    2023-02-28 10:39:46
  • android选择视频文件上传到后台服务器

    2023-06-11 22:50:44
  • 通过WIFI(不用数据线)连接Android手机调试

    2023-01-05 18:20:45
  • 讲解使用Docker搭建Java Web运行环境

    2023-02-05 15:58:12
  • asp之家 软件编程 m.aspxhome.com