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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了asp.net生成缩略图示例方法分享脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

代码:

新建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);
                }
            }
        }

脚本宝典总结

以上是脚本宝典为你收集整理的asp.net生成缩略图示例方法分享全部内容,希望文章能够帮你解决asp.net生成缩略图示例方法分享所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: