asp.net处理网络返回图片

本文参考糅合自以下几个网站:

1.参考输出流:http://blog.csdn.net/lusend/article/details/8299398

2.参考二进制流转换(本文没用到):http://developer.51cto.com/art/200908/143612.htm

3.参考bitmap和image之间格式转换:http://www.cnblogs.com/peasana/archive/2012/02/13/2349165.html

4.参考页面不存在的时候返回图片:http://www.cnblogs.com/lema/archive/2010/12/10/1902679.html

5.参考返回无效图片时候,httpcontext用法:http://www.cnblogs.com/fish-li/archive/2013/04/06/3002940.html

6.图片处理主要参考自(这个可以用于,我们本地电脑上的图片处理,画图不好用的时候可以参考这个):http://www.cnblogs.com/qfb620/archive/2010/05/22/1741519.html

不得不说,我对c#略微熟悉,对于asp.net来说,完全是没接触过,虽然传说2个本质上没区别,无奈接了个同事的要求,帮忙处理网络上的大图片,到手机上显示小图。搞了整整半天,才知道我在做的是asp.net,之前的半天,连项目也没建立起来,这种情况下,不能说我这个小白能够完全处理好,达到了理想状态,至少,侥幸能把上百K的图片,变成3到5k了,还是有不少收获的,参考了好多大牛写的文章,开篇已经列出。本文就是对上文文章剪剪裁裁获得的。

一篇好的代码不需要过多注释,相信聪明的你一定能够看懂。

private const string DefaultimageUrl = "Default.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            string a = Request.QueryString.Get("id");//这个是指的,传参的时候必须上?id=xx,不然就无效
            HttpR(a);
        }

        private void HttpR(string id)
        {
            string uri = "http://xxx" + id + ".jpg";
            WebRequest wreq = WebRequest.Create(uri);
            try
            {
                var wresq = (HttpWebResponse) wreq.GetResponse();
                Stream stream = wresq.GetResponseStream();
                if (stream != null)
                {
                    Image img = Image.FromStream(stream);
                    Convert(img);
                }
            }
            catch (Exception)
            {
                ProcessRequest();
            }
        }

        //输出默认图片
        private static void ProcessRequest()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                Image image = Image.FromFile(context.Server.MapPath(DefaultimageUrl));
                context.Response.ContentType = "image/jpeg";
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                image.Dispose();
            }
        }

        private void Convert(Image picPath)
        {
            var originBmp = (Bitmap) picPath;
            //根据需求调整宽高
            const int width = 80, hight = 110;
            //绘制图片
            var resizedBmp = new Bitmap(width, hight);
            Graphics g = Graphics.FromImage(resizedBmp);
            g.InterpolationMode = InterpolationMode.Low;
            g.SmoothingMode = SmoothingMode.Default;
            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.SmoothingMode = SmoothingMode.HighSpeed;
            g.DrawImage(originBmp, new Rectangle(0, 0, width, hight),
                new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
            //输出图片
            var ms = new MemoryStream();
            resizedBmp.Save(ms, ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
            //释放所有内存流
            g.Dispose();
            resizedBmp.Dispose();
            originBmp.Dispose();
            ms.Dispose();
        }

  

 

posted on 2014-05-23 15:06  鸣动我心  阅读(942)  评论(0编辑  收藏  举报