现在的位置: 首页 > 综合 > 正文

C# WinForm 通过URL取得服务器上的某图片文件到本地

2012年04月03日 ⁄ 综合 ⁄ 共 1486字 ⁄ 字号 评论关闭

方法1示例代码:
--------------

 

string strImageURL = "http://192.168.0.1:88/VDirA/images/1.jpg";

System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile(strImageURL, @"D:\1.jpg"); 

 

 

方法2示例代码:
--------------

string strImageURL = "http://192.168.0.1:88/VDirA/images/1.jpg";

System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strImageURL);
webRequest.Method = "GET";
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();

System.IO.Stream s = webResponse.GetResponseStream();

List<byte> list = new List<byte>();
while (true)
{
    int data = s.ReadByte();
    if (data == -1)
        break;
    else
    {
        byte b = (byte)data;
        list.Add(b);
    }
}
byte[] bb = list.ToArray();
System.IO.File.WriteAllBytes("C://1.jpg", bb);
s.Close();

 

 

 

方法1示例代码:
--------------

 

string strImageURL = "http://192.168.0.1:88/VDirA/images/1.jpg";

System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile(strImageURL, @"D:\1.jpg"); 

 

 

方法2示例代码:
--------------

string strImageURL = "http://192.168.0.1:88/VDirA/images/1.jpg";

System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strImageURL);
webRequest.Method = "GET";
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();

System.IO.Stream s = webResponse.GetResponseStream();

List<byte> list = new List<byte>();
while (true)
{
    int data = s.ReadByte();
    if (data == -1)
        break;
    else
    {
        byte b = (byte)data;
        list.Add(b);
    }
}
byte[] bb = list.ToArray();
System.IO.File.WriteAllBytes("C://1.jpg", bb);
s.Close();

 

 

 

抱歉!评论已关闭.