码迷,mamicode.com
首页 > Web开发 > 详细

asp.net处理网络返回图片

时间:2014-05-26 07:01:39      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   class   c   blog   code   tar   

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

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了,还是有不少收获的,参考了好多大牛写的文章,开篇已经列出。本文就是对上文文章剪剪裁裁获得的。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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();
        }

  

 

asp.net处理网络返回图片,布布扣,bubuko.com

asp.net处理网络返回图片

标签:style   class   c   blog   code   tar   

原文地址:http://www.cnblogs.com/defoliate/p/3747772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!