ASP.NET中高质量缩略图的生成代码

35 篇文章 0 订阅
25 篇文章 0 订阅

ASP.NET中高质量缩略图的生成代码

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_336_280.htm" frameborder="0" width="340" scrolling="no" height="282">

    private Size NewSize(int maxWidth, int maxHeight, int width, int height)
    {
        double w = 0.0;
        double h = 0.0;
        double sw = Convert.ToDouble(width);
        double sh = Convert.ToDouble(height);
        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 Size(Convert.ToInt32(w), Convert.ToInt32(h));
    }

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

    private void SendSmallImage(string fileName, int maxWidth, int maxHeight)
    {
        System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(fileName));
        System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
        Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
        Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
        Graphics g = Graphics.FromImage(outBmp);
        // 设置画布的描绘质量
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
            0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
        g.Dispose();
        if (thisFormat.Equals(ImageFormat.Gif))
        {
            Response.ContentType = "image/gif";
        }
        else
        {
            Response.ContentType = "image/jpeg";
        }

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

        // 以下代码为保存图片时,设置压缩质量
        EncoderParameters encoderParams = new EncoderParameters();
        long[] quality = new long[1];
        quality[0] = 100;
        EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        encoderParams.Param[0] = encoderParam;
        //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
        ImageCodecInfo jpegICI = null;
        for (int x = 0; x < arrayICI.Length; x++)
        {
            if (arrayICI[x].FormatDescription.Equals("JPEG"))
            {
                jpegICI = arrayICI[x];//设置JPEG编码
                break;
            }
        }
        if (jpegICI != null)
        {
            outBmp.Save(Response.OutputStream, jpegICI, encoderParams);
        }
        else
        {
            outBmp.Save(Response.OutputStream, thisFormat);
        }
        img.Dispose();
        outBmp.Dispose();

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_ads_468_15.htm" frameborder="0" width="470" scrolling="no" height="18">

border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_syndication_468_60.htm" frameborder="0" width="470" scrolling="no" height="62">

搜索其它相关资源,请用Google搜索:
border="0" name="I1" align="middle" marginwidth="1" marginheight="1" src="http://www.b199.cn/blog_ads/Google_search_40.htm" frameborder="0" width="460" scrolling="no" height="40">

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值