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

c#根据路径(url)下载图片

程序员文章站 2022-03-30 22:16:04
方法二:根据路径下载图片 方法三 根据路径下载图片 ......

方法一:根据路径下载图片

1 /// <summary> 2 /// 图片另存为 3 /// </summary> 4 /// <param name="url">路径</param> 5 public void imgsave(string url) 6 { 7   //http://203.156.245.58/sipgl/index.jsp 8   url = "http://203.156.245.58/sipgl/login/img"; 9   webrequest imgrequest = webrequest.create(url); 10 11   httpwebresponse res; 12   try 13   { 14     res = (httpwebresponse)imgrequest.getresponse(); 15   } 16     catch (webexception ex) 17   { 18 19   res = (httpwebresponse)ex.response; 20   } 21 22   if (res.statuscode.tostring() == "ok") 23   { 24     system.drawing.image downimage = system.drawing.image.fromstream(imgrequest.getresponse().getresponsestream()); 25 26     string deerory = string.format(@"d:\img\{0}\", datetime.now.tostring("yyyy-mm-dd")); 27 28     string filename = string.format("{0}.png", datetime.now.tostring("hhmmssffff")); 29 30 31   if (!system.io.directory.exists(deerory)) 32   { 33     system.io.directory.createdirectory(deerory); 34   } 35     downimage.save(deerory + filename); 36     downimage.dispose(); 37   } 38 39 } 40 41

 

 

c#根据路径(url)下载图片

 

方法二:根据路径下载图片 

 

 1      /// <summary>
 2         /// 下载图片
 3         /// </summary>
 4         /// <param name="picurl">图片http地址</param>
 5         /// <param name="savepath">保存路径</param>
 6         /// <param name="timeout">request最大请求时间,如果为-1则无限制</param>
 7         /// <returns></returns>
 8         public  bool downloadpicture(string picurl, string savepath, int timeout)
 9         {
10             picurl = "http://203.156.245.58/sipgl/login/img";
11             savepath = "d:/img/"+datetime.now.tostring("hhmmssffff")+".jpg";
12             bool value = false;
13             webresponse response = null;
14             stream stream = null;
15             try
16             {
17                 httpwebrequest request = (httpwebrequest)webrequest.create(picurl);
18                 if (timeout != -1) request.timeout = timeout;
19                 response = request.getresponse();
20                 stream = response.getresponsestream();
21                 if (!response.contenttype.tolower().startswith("text/"))
22                     value = savebinaryfile(response, savepath);
23             }
24             finally
25             {
26                 if (stream != null) stream.close();
27                 if (response != null) response.close();
28             }
29             return value;
30         }
31         private static bool savebinaryfile(webresponse response, string savepath)
32         {
33             bool value = false;
34             byte[] buffer = new byte[1024];
35             stream outstream = null;
36             stream instream = null;
37             try
38             {
39                 if (file.exists(savepath)) file.delete(savepath);
40                 outstream = system.io.file.create(savepath);
41                 instream = response.getresponsestream();
42                 int l;
43                 do
44                 {
45                     l = instream.read(buffer, 0, buffer.length);
46                     if (l > 0) outstream.write(buffer, 0, l);
47                 } while (l > 0);
48                 value = true;
49             }
50             finally
51             {
52                 if (outstream != null) outstream.close();
53                 if (instream != null) instream.close();
54             }
55             return value;
56         }
c#根据路径(url)下载图片

 

c#根据路径(url)下载图片

 

方法三  根据路径下载图片

 

 1     public image getimage(string url,out string imagestrcookie)
 2         {
 3            
 4             httpwebrequest request = (httpwebrequest)webrequest.create("http://203.156.245.58/sipgl/login/img");
 5             request.method = "get";
 6             webresponse response = request.getresponse();
 7             imagestrcookie = "";
 8             if (response.headers.haskeys()&&null !=response.headers["set-cookie"])
 9             {
10                 imagestrcookie= response.headers.get("set-cookie");
11             }
12             return image.fromstream(response.getresponsestream());
13             
14         }

c#根据路径(url)下载图片