对给定的一个图片生成一个指定大小的缩略图(上传图片很大是可用)

 #region##对给定的一个图片(Image对象)生成一个指定大小的缩略图
    ///<summary>
    /// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。
    ///</summary>
    ///<param name="originalImage">原始图片</param>
    ///<param name="thumMaxWidth">缩略图的宽度</param>
    ///<param name="thumMaxHeight">缩略图的高度</param>
    ///<returns>返回缩略图的Image对象</returns>
    public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
    {
        System.Drawing.Size thumRealSize = System.Drawing.Size.Empty;
        System.Drawing.Image newImage = originalImage;
        System.Drawing.Graphics graphics = null;
        try
        {
            thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
            newImage = new System.Drawing.Bitmap(thumRealSize.Width, thumRealSize.Height);
            graphics = System.Drawing.Graphics.FromImage(newImage);
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphics.Clear(System.Drawing.Color.Transparent);
            graphics.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new System.Drawing.Rectangle(0, 0, originalImage.Width, originalImage.Height), System.Drawing.GraphicsUnit.Pixel);
        }
        catch { }
        finally
        {
            if (graphics != null)
            {
                graphics.Dispose();
                graphics = null;
            }
        }
        return newImage;
    }
    #endregion

    #region##获取一个图片按等比例缩小后的大小
    ///<summary>
    /// 获取一个图片按等比例缩小后的大小。
    ///</summary>
    ///<param name="maxWidth">需要缩小到的宽度</param>
    ///<param name="maxHeight">需要缩小到的高度</param>
    ///<param name="imageOriginalWidth">图片的原始宽度</param>
    ///<param name="imageOriginalHeight">图片的原始高度</param>
    ///<returns>返回图片按等比例缩小后的实际大小</returns>
    public static System.Drawing.Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
    {
        double w = 0.0;
        double h = 0.0;
        double sw = Convert.ToDouble(imageOriginalWidth);
        double sh = Convert.ToDouble(imageOriginalHeight);
        double mw = Convert.ToDouble(maxWidth);
        double mh = Convert.ToDouble(maxHeight);
        if (sw < mw && sh < mh)
        {
            w = sw;
            h = sh;
        }
        else if ((sw / sh) > (mw / mh))
        {
            w = maxWidth;
            h = (w * sh) / sw;
        }
        else
        {
            h = maxHeight;
            w = (h * sw) / sh;
        }
        return new System.Drawing.Size(Convert.ToInt32(w), Convert.ToInt32(h));
    }
    #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值