您的位置:首页 > 其它

.NET下载网页图片

2016-07-26 18:08 281 查看
两种方式:WebRequest 和 WebClient:
 
public void DownloadImage(string url, string path)
{
WebRequest request = WebRequest.Create(url);
request.ContentType = "impage/jpg";
WebResponse response = request.GetResponse();
using (Stream reader = response.GetResponseStream())
{
using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[512];
int c = 0;
while ((c = reader.Read(buffer, 0, buffer.Length)) > 0)
{
writer.Write(buffer, 0, c);
}
}
}

}

//public void DownloadImage(string url, string path)
//{
//    WebClient client = new WebClient();
//    client.DownloadFile(url, path);
//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: