【求助】c#如何用WebRequest下载一张网上的图片[顶者有分]

zagj11 2009-12-30 05:56:14
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strURL);

给个思路。。谢谢。
...全文
278 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ai2015WER 2011-04-19
  • 打赏
  • 举报
回复
高手呀....
qq153325250 2009-12-30
  • 打赏
  • 举报
回复
顶过
zagj11 2009-12-30
  • 打赏
  • 举报
回复
哈哈。都是才子。结贴了。感谢各位。。。
mngzilin 2009-12-30
  • 打赏
  • 举报
回复
四行代码就可以:

string strURL = "http://avatar.profile.csdn.net/4/1/A/2_yingkk.jpg";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strURL);
System.Drawing.Image img = System.Drawing.Image.FromStream(req.GetResponse().GetResponseStream());
img.Save("c:\\1.jpg");
xiaovsjia 2009-12-30
  • 打赏
  • 举报
回复
呵呵,为了分,我来了
  • 打赏
  • 举报
回复
找到网页里的img src=之类的东西(用正则),然后在网址后面加上图片的路径,直接用WebClient下载之



楼下的继续顶,有分
wuyq11 2009-12-30
  • 打赏
  • 举报
回复
public static void DownloadOneFileByURL(string fileName, string url, string localPath, int timeout)
{
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url + fileName);
request.Timeout = timeout;
response = (System.Net.HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
BinaryReader br = new BinaryReader(s);
int len= Int32.Parse(response.ContentLength.ToString());
byte[] byteArr = new byte[len];
s.Read(byteArr, 0, len);
if (File.Exists(localPath + fileName)) { File.Delete(localPath + fileName); }
if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); }
FileStream fs = File.Create(localPath + fileName);
fs.Write(byteArr, 0, len);
fs.Close();
br.Close();
}
或用webclient
yingkk 2009-12-30
  • 打赏
  • 举报
回复
这是我做验证码的时候用来读取图片的,具体是从哪参考来的记不得了。

MemoryStream stream = new MemoryStream(downloadData(tb_url.Text));
Image img= Image.FromStream(stream);


private byte[] downloadData(string url)
{
byte[] downloadedData = new byte[0];
try
{
//Optional
this.lb_status.Text = "Connecting...";
Application.DoEvents();

//Get a data stream from the url
WebRequest req = WebRequest.Create(url);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();

//Download in chuncks
byte[] buffer = new byte[1024];

//Get Total Size
int dataLength = (int)response.ContentLength;

//With the total data we can set up our progress indicators

this.lb_status.Text = "Downloading...";
Application.DoEvents();

//Download to memory
//Note: adjust the streams here to download directly to the hard drive
MemoryStream memStream = new MemoryStream();
while (true)
{
//Try to read the data
int bytesRead = stream.Read(buffer, 0, buffer.Length);

if (bytesRead == 0)
{
Application.DoEvents();
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
}
}

//Convert the downloaded stream to a byte array
downloadedData = memStream.ToArray();

//Clean up
stream.Close();
memStream.Close();
}
catch (Exception)
{
//May not be connected to the internet
//Or the URL might not exist
MessageBox.Show("There was an error accessing the URL.");
}
this.lb_status.Text = "Download Data through HTTP";
return downloadedData;
}
bingo_ 2009-12-30
  • 打赏
  • 举报
回复

void DownLoadToFile(string Url, string Path)
{
WebRequest HWR = WebRequest.Create(Url);
WebResponse HWRr = HWR.GetResponse();

Stream SR = HWRr.GetResponseStream();
FileStream S = File.Open(Path, FileMode.Create);
byte[] by = new byte[1024];
int o=1;
while (o > 0)
{
o = SR.Read(by, 0, 1024);
S.Write(by, 0, o);
}
S.Close();
SR.Close();
S.Dispose();
SR.Dispose();
HWR = null;
HWRr = null;
}
bingo_ 2009-12-30
  • 打赏
  • 举报
回复

kensouterry 2009-12-30
  • 打赏
  • 举报
回复
顺便顶一下了!
kensouterry 2009-12-30
  • 打赏
  • 举报
回复
那我Mark一下,回去再看!

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧