主题:  ASP.NET中高质量缩略图的生成

蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#12005/10/14 17:12:09
最近做一摄影作品管理程序,用到自动生成缩略图的方法。开始方法较简单,用GDI+中默认方法,但生成图片质量不佳,并且压缩质量为中等。
潜心研究了一下,找到以下方法,主要分二布,第一步为画布描绘时的质量设置,第二步为保存图片时JPEG压缩的设置。

代码如下:


        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();
        }

编辑历史:[此帖最近一次被 蓝鲸 编辑过(编辑时间:2005-10-15 21:38:02)]

非常大鱼

缺缺

职务:管理员
等级:8
金币:41.0
发贴:9620
#22005/10/14 17:37:14
不错.我曾经也为图片的质量烦恼过~不错.net的图片压缩好像不太好.有时缩略图比原图还大



蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#32005/10/14 17:53:06
图像质量对比

没有优化的:

图片如下:


优化后的图片:

图片如下:


非常大鱼

Steveson

职务:普通成员
等级:1
金币:0.0
发贴:68
#42005/10/26 15:02:11
生成的缩略图是不是在本地路径上打不开啊(显示是"X",而用网络路径是可以打开的,
是不是因为压缩的原因啊(查看文件的属性是"PNG",
有没有办法让图片变小但不是压缩格式的,