用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > Net教程

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

时间:2015-01-29  来源:互联网  作者:佚名
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));
        }

        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";
            }

            // 以下代码为保存图片时,设置压缩质量
            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();
        }

来顶一下
返回首页
返回首页
推荐资讯
vs 不显示行号的操作方法 vs 不显示行号的操作方法工具---->选项---->文本编辑器---->c#---->常规---->显示---
让VS2008对JQuery语法的智能感知更完美一点 让VS2008对JQuery语法的智能感知更一、刚才看到已经有快手的牛人做完了这项工作,现在可以直接
相关文章
    无相关信息
栏目更新
栏目热门