asp.net抓取远程图片

1.利用WebRequest,WebResponse 类

 

 1             WebRequest wreq = WebRequest.Create("http://www.baidu.com/img/logo-yy.gif");
 2             HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
 3             Stream s = wresp.GetResponseStream();
 4             System.Drawing.Image img;
 5             img = System.Drawing.Image.FromStream(s);
 6             img.Save("D:\\aa.gif", ImageFormat.Gif);   //保存 
 7             //下面直接输出 
 8             MemoryStream ms = new MemoryStream();
 9             img.Save(ms, ImageFormat.Gif);
10             img.Dispose();
11             Response.ClearContent();
12             Response.ContentType = "image/gif";
13             Response.BinaryWrite(ms.ToArray()); 


2.利用 WebClient 类

 

 1             WebClient my = new WebClient();
 2             byte[] mybyte;
 3             mybyte = my.DownloadData("http://www.baidu.com/img/logo-yy.gif"); MemoryStream ms = new MemoryStream(mybyte);
 4             System.Drawing.Image img;
 5             img = System.Drawing.Image.FromStream(ms);
 6             img.Save("D:\\a.gif", ImageFormat.Gif);   //保存 
 7             //下面直接输出 
 8             Response.ClearContent();
 9             Response.ContentType = "image/gif";
10             Response.BinaryWrite(mybyte);
11             //如果是真实的图片地址直接用 
12             my.DownloadFile("http://www.baidu.com/img/logo-yy.gif""D:\\a.gif");
13             //保存直接可以保存
14             WebRequest request = WebRequest.Create("upload/200904022000525545.gif");
15             WebResponse response = request.GetResponse();
16             Stream reader = response.GetResponseStream();
17             FileStream writer = new FileStream("D:\\logo.gif", FileMode.OpenOrCreate, FileAccess.Write);
18             byte[] buff = new byte[512];
19             int c = 0//实际读取的字节数
20             while ((c = reader.Read(buff, 0, buff.Length)) > 0)
21             {
22                 writer.Write(buff, 0, c);
23             }
24             writer.Close();

 

posted @ 2011-05-24 18:44  特务小强  阅读(2080)  评论(2编辑  收藏  举报