title: Image scraper with C#
body: <p>I'm trying to go through a web pages source code, add the <code>&lt;img src="http://www.dot.com/image.jpg"</code> to an <code>HtmlElementCollection</code>. Then I'm attempting to cycle through each element in the element collection with a foreach loop and download the images through the url.</p>
  
  <p>Here's what I have so far. My problem right now is nothing is downloading, and I don't think my elements are being added properly by tag name. If they are I can't seem to reference them for the download.</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      public void button1_Click(object sender, EventArgs e)
      {
          string url = urlTextBox.Text;
          string sourceCode = WorkerClass.ScreenScrape(url);
          StreamWriter sw = new StreamWriter("sourceScraped.html");
          sw.Write(sourceCode);
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          string url = urlTextBox.Text;
          WebBrowser browser = new WebBrowser();
          browser.Navigate(url);
          HtmlElementCollection collection;
          List&lt;HtmlElement&gt; imgListString = new List&lt;HtmlElement&gt;();
          if (browser != null)
          {
              if (browser.Document != null)
              {
                  collection = browser.Document.GetElementsByTagName("img");
                  if (collection != null)
                  {
                      foreach (HtmlElement element in collection)
                      {
                          WebClient wClient = new WebClient();
                          string urlDownload = element.FirstChild.GetAttribute("src");
                          wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf('/')));
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>