当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET中高质量缩略图的生成代码

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

2018年04月25日  | 移动技术网IT编程  | 我要评论
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();
        }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网