dimanche 22 février 2015

C# How to download all images in a document

I am in the process of creating a web browser in C# however, I cannot find a way to download all images on a certain web page to my local disk. When I navigate to my page and click on my button to download after a few seconds it says 'an exception occurred during a webclient request'. My code I am using is



WebClient wc = new WebClient();
HtmlElementCollection imgs = this.webBrowser1.Document.GetElementsByTagName("img");

for (int i = 1; i < imgs.Count; i++)
{
wc.DownloadFile(imgs[i].GetAttribute("src"),
@"C:\Users\EugeneProut\Desktop\images\" + i.ToString() + ".jpg" );
}
wc.Dispose();


The exception I am getting as as follows



System.Net.WebException: An exception occurred during a WebClient request.
---> System.NotSupportedException: The URI prefix is not recognized.
at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
at System.Net.WebRequest.Create(Uri requestUri)
at System.Net.WebClient.GetWebRequest(Uri address)
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
--- End of inner exception stack trace ---


Code snippets would be best.


Aucun commentaire:

Enregistrer un commentaire