所以我昨晚刚开始学习C#.我开始的第一个项目是一个简单的Image-Downloader,它使用HtmlElementCollection下载网站的所有图像.
这是我到目前为止所得到的:
private void dl_Click(object sender, EventArgs e)
{
System.Net.WebClient wClient = new System.Net.WebClient();
HtmlElementCollection hecImages = Browser.Document.GetElementsByTagName("img");
for (int i = 0; i < hecImages.Count - 1; i++)
{
char[] ftype = new char[4];
string gtype;
try
{
//filetype
hecImages[i].GetAttribute("src").CopyTo(hecImages[i].GetAttribute("src").Length -4,ftype,0,4) ;
gtype = new string(ftype);
//copy image to local path
wClient.DownloadFile(hecImages[i].GetAttribute("src"), absPath + i.ToString() + gtype);
}
catch (System.Net.WebException)
{
expand_Exception_Log();
System.Threading.Thread.Sleep(50);
}
Run Code Online (Sandbox Code Playgroud)
基本上它是提前渲染页面并寻找图像.这很好用,但由于某种原因它只下载缩略图,而不是完整(高分辨率)图像.
其他来源:
WebClient.DownloadFile上的文档:http://msdn.microsoft.com/en-us/library/ez801hhe( v = vs.110).aspx
DownloadFile方法从address参数中指定的URI下载到本地文件数据.