.net 上传图片进行缩放

本文介绍了一种图片缩放处理的方法,可以根据不同的需求选择固定的尺寸缩放、按比例缩放或根据图片原始尺寸进行智能调整。该方法适用于多种应用场景,如网站图片优化、图像处理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文中来源于网络,仅供参考
/// <summary>
/// 上传图片进行缩放
/// </summary>
/// <param name="fileName">图片绝对地址</param>
/// <param name="destWidth">指定宽度</param>
/// <param name="destHeight">指定高度</param>
/// <param name="type">1--固定缩放;2--按比例缩放;3--指定宽度,宽度大于指定宽度按指定宽度进行等比缩放,小于指定宽度按原图大小上传;4--原图直接上传</param>
public  void ReducesPic(string fileName, int destWidth, int destHeight, int type)
{
    if (!fileName.Equals(""))
    {
        //图片决定路径
        string Allpath = fileName;
        //生成原图
        System.IO.Stream stream = System.IO.File.OpenRead(Allpath);
        System.Drawing.Image oImage = System.Drawing.Image.FromStream(stream);
        stream.Close();
        stream.Dispose();

        System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

        string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
        int oWidth = oImage.Width;
        int oHeight = oImage.Height;

        int tWidth = destWidth; //设置缩略图初始宽度
        int tHeight = destHeight; //设置缩略图初始高度

        //按指定宽高缩放
        if (type == 1)
        {
            tWidth = destWidth;
            tHeight = destHeight;
        }
        //按比例计算出缩略图的宽度和高度
        else if (type == 2)
        {
            if (oWidth > tWidth || oHeight > tHeight)
            {
                if (oWidth >= oHeight)
                {
                    tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
                }
                else
                {
                    tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                }
            }
            else
            {
                tWidth = oWidth; //原图宽度
                tHeight = oHeight; //原图高度
            }
        }
        //指定宽度,宽度大于指定宽度按指定宽度进行等比缩放,小于指定宽度按原图大小上传
        else if (type == 3)
        {
            if (oWidth >= tWidth)
            {
                if (oWidth >= oHeight)
                {
                    tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
                }
                else
                {
                    tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
                }
            }
            else
            {
                tWidth = oWidth; //原图宽度
                tHeight = oHeight; //原图高度
            }

        }
        else
        {
            tWidth = oWidth; //原图宽度
            tHeight = oHeight; //原图高度
        }
        //生成缩略原图
        oImage = oImage.GetThumbnailImage(tWidth, tHeight, callb, IntPtr.Zero);
        oImage.Save(Allpath);

    }
}
public static bool ThumbnailCallback() { return false; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值