Windows Develop Bookmark and Share   
 index > Windows Forms General > Grab images in a website
 

Grab images in a website

hi,
i need to create an application that hav to download all images in a website.
How can i achieve that...???
thanks in advance

regards
faisal
faisalpar  Saturday, October 03, 2009 10:35 AM
Project + Reference, select Microsoft.mshtml.  This sample code retrieves all the image urls from google.com.  Beware of duplicates, you'll need a dictionary.

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
      webBrowser1.Url = new Uri("http://google.com");
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
      HtmlDocument doc = webBrowser1.Document;
      foreach (HtmlElement tag in doc.Body.All) {
        if (tag.TagName == "IMG") {
          mshtml.IHTMLImgElement img = tag.DomElement as mshtml.IHTMLImgElement;
          string imgUrl = img.href;
          // download image with WebClient.DownloadXxx() here
          //..
          Console.WriteLine(imgUrl);
        }
      }
    }
  }

Hans Passant.
nobugz  Saturday, October 03, 2009 2:35 PM
doubtful, the HTTP standard does not enumerate a web site. You can know a file exists if the image is referenced by a webpage, but you can not know if there are some imaghe not referenced. 

Besides, the image can be regenerated in a program like a stock trend page tht embed on the page as different url. So there can be unlimited amount of images.

Anyway, Windows Forms is a UI library. Your question may draw more interest in a network programming community.


The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Saturday, October 03, 2009 2:09 PM
Project + Reference, select Microsoft.mshtml.  This sample code retrieves all the image urls from google.com.  Beware of duplicates, you'll need a dictionary.

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
      webBrowser1.Url = new Uri("http://google.com");
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
      HtmlDocument doc = webBrowser1.Document;
      foreach (HtmlElement tag in doc.Body.All) {
        if (tag.TagName == "IMG") {
          mshtml.IHTMLImgElement img = tag.DomElement as mshtml.IHTMLImgElement;
          string imgUrl = img.href;
          // download image with WebClient.DownloadXxx() here
          //..
          Console.WriteLine(imgUrl);
        }
      }
    }
  }

Hans Passant.
nobugz  Saturday, October 03, 2009 2:35 PM

You can use google to search for other answers

Custom Search

More Threads

• How to change DataGridView scrollbar color
• Automatically setting wait cursor for application
• printing text
• OwnerDraw TextBox
• Windows form: keydown event doesn't capture Tab key.
• Cursor is Where?
• Window Vista
• TreeView Backcolor bug
• Adding Controls to a User control
• WndProc and GetMessageExtraInfo