欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net生成缩略图示例方法分享

程序员文章站 2024-02-28 14:52:28
做站的时候经常会遇到要生成缩略图的功能,因为可能不同的情况需要用来不同大小的缩略图。 本文生成的图片都为正方形,只有正方形的缩略图才是保证图片足够清晰。 当我我这里说的...

做站的时候经常会遇到要生成缩略图的功能,因为可能不同的情况需要用来不同大小的缩略图。

本文生成的图片都为正方形,只有正方形的缩略图才是保证图片足够清晰。

当我我这里说的正方形是先按比例压缩,然后加一个固定的白底 然后居中显示。

代码:

新建outputimg.ashx

复制代码 代码如下:

//调整图片大小
private static 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)//如果maxwidth和maxheight大于源图像,则缩略图的长和高不变
            {
                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));
        }

复制代码 代码如下:

//生成缩略图
public static void sendsmallimage(string filename, string newfile, int maxheight, int maxwidth, string mode)
        {
            system.drawing.image img = system.drawing.image.fromfile(filename);//源图像的信息
            system.drawing.imaging.imageformat thisformat = img.rawformat; //源图像的格式
            size newsize = newsize(maxwidth, maxheight, img.width, img.height); //返回调整后的图像width与height
            bitmap outbmp = new bitmap(maxwidth, maxheight);
            graphics g = graphics.fromimage(outbmp);
            //设置画布的描绘质量
            g.compositingquality = compositingquality.highquality;
            g.smoothingmode = smoothingmode.highquality;
            g.interpolationmode = interpolationmode.highqualitybicubic;
            g.clear(color.white);
            g.drawimage(img, new rectangle(((maxwidth - newsize.width) / 2), ((maxheight - newsize.height) / 2), newsize.width, newsize.height), 0, 0, img.width, img.height, graphicsunit.pixel);
            g.dispose();
            //以下代码为保存图片时,设置压缩质量
            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(newfile, jpegici, encoderparams);
            }
            else
            {
                outbmp.save(newfile, thisformat);
            }
            img.dispose();
            outbmp.dispose();
        }

输出图片:

复制代码 代码如下:

//输出图片
        public static void outputimg(string imgfilepath)
        {
            filestream fs = new filestream(httpcontext.current.server.mappath(imgfilepath), filemode.open, fileaccess.read);
            datetime contentmodified = system.io.file.getlastwritetime(httpcontext.current.server.mappath(imgfilepath));
            if (isclientcached(contentmodified))
            {
                httpcontext.current.response.statuscode = 304;
                httpcontext.current.response.suppresscontent = true;
            }
            else
            {
                byte[] mydata = new byte[fs.length];
                int length = convert.toint32(fs.length);
                fs.read(mydata, 0, length);
                fs.close();
                httpcontext.current.response.outputstream.write(mydata, 0, length);
                httpcontext.current.response.contenttype = "image/jpeg";
                httpcontext.current.response.end();
                httpcontext.current.response.cache.setetagfromfiledependencies();
                httpcontext.current.response.cache.setallowresponseinbrowserhistory(true);
                httpcontext.current.response.cache.setlastmodified(contentmodified);
            }
        }

复制代码 代码如下:

//outpuimg.ashx?src=/images/weimeidesc/8af30049-797e-4eb4-8a54-cc4de47c1694.jpg!100x100.jpg
        public void processrequest(httpcontext context)
        {
            //获取图片
            string imgurl = context.request.querystring["src"];
            string truefilepath = imgurl.split('!')[0];
            //获取图片大小
            int width = convert.toint32(imgurl.split('!')[1].replace(".jpg", "").split('x')[0]);
            int height = convert.toint32(imgurl.split('!')[1].replace(".jpg", "").split('x')[1]);

            //图片已经存在直接输出
            if (file.exists(context.server.mappath("~/" + imgurl)))
            {
                outputimg("~/"+imgurl);
            }
            else
            {
                if (!string.isnullorempty(imgurl) && file.exists(context.server.mappath("~/" + truefilepath)))
                {
                    image originalimage = system.drawing.image.fromfile(context.server.mappath("~/" + truefilepath));
                    var newbitmap = new bitmap(originalimage);
                    //生成相应的小图并保存
                    sendsmallimage(context.server.mappath("~/" + truefilepath),context.server.mappath("~/" + imgurl), width, height, "meiyouyisi");
                    //输出
                    outputimg("~/" + imgurl);
                }
                else//图片如果不存在 输出默认图片
                {
                    //outputimg(imgurl);
                }
            }
        }