How to use Visual Studio 2012 to Download Images from Websites


As you might know, I have always been fascinated with Wallpapers and photos. What you might not know is that I appreciate good art as well, seeing the fact that my wife is quite good at painting and creating things out of nothing.

I have a huge collection of wallpapers. Some of you might remember this old dinosaur project of mine, which I still use successfully to change my desktop backgrounds. Because of the tedious nature of collecting new wallpapers, I have decided to make a little tool to do all the hard work for me. With this article I will demonstrate how to download images from a webpage. Note: some pictures are copyrighted, and must not be used in any way. I am not advocating that. I am using pictures from websites that provide them for free.

Anyways, enough about me, let us get started with our little project


Fire up Visual Studio 2012 and choose your desired programming language. I will illustrate both VB.NET as well as C#.

Design your form to resemble Figure 1.

Our Design
Figure 1 - Our Design


Not much coding needed from our side. Let's start with importing the Namespaces.


Imports System.IO 'File Functions


using System.IO; //For File Functions

Now let's handle the navigation button that will enable us to navigate to a site:


    Private Sub btnIDGo_Click(sender As Object, e As EventArgs) Handles btnIDGo.Click

        wbID.Navigate(txtIDURL.Text) 'Navigate To URL

    End Sub


        private void btnIDGo_Click(object sender, EventArgs e)
            wbID.Navigate(txtIDURL.Text); //Navigate To URL


The above code segment instructs the WebBrowser Control to navigate to the entered site. This also assumes your WebBrowser Control was named wbID as was the case with mine.

Now the fun starts!

Add the next code segment:


    Private Sub btnIDGet_Click(sender As Object, e As EventArgs) Handles btnIDGet.Click

        Dim wcObj As New System.Net.WebClient() 'Create New Web Client Object

        Dim hecImages As HtmlElementCollection = wbID.Document.GetElementsByTagName("img") 'Browse Through HTML Tags

        Dim strWebTitle As String 'Web Page Title
        Dim strPath1 As String 'Folder Path
        Dim strPath2 As String 'Sub Folder Path

        strWebTitle = Me.wbID.DocumentTitle  'Obtain Web Page Title

        strPath1 = "c:\" & strWebTitle 'Create Folder Named Web Page Title
        strPath2 = strPath1 & "\Images" 'Create Images Sub Folder

        Dim diTitle As DirectoryInfo = Directory.CreateDirectory(strPath1)
        Dim diImages As DirectoryInfo = Directory.CreateDirectory(strPath2)

        For i As Integer = 0 To hecImages.Count - 1 'Loop Through All IMG Elements Found

            'Download Image(s) To Specified Path

            wcObj.DownloadFile(hecImages(i).GetAttribute("src"), strPath1 & "\images\" & i.ToString() & ".jpg")

    End Sub


        private void btnIDGet_Click(object sender, EventArgs e)

            System.Net.WebClient wcObj = new System.Net.WebClient(); //Create New Web Client Object

            HtmlElementCollection hecImages = wbID.Document.GetElementsByTagName("img"); //Browse Through HTML Tags

            string strWebTitle; //Web Page Title
            string strPath1; //Folder Path
            string strPath2; //Sub Folder Path

            strWebTitle = this.wbID.DocumentTitle; //Obtain Web Page Title

            strPath1 = @"c:\" + strWebTitle; //Create Folder Named Web Page Title
            strPath2 = strPath1 + "\\Images"; //Create Images Sub Folder

            DirectoryInfo diTitle = Directory.CreateDirectory(strPath1);
            DirectoryInfo diImages = Directory.CreateDirectory(strPath2);

            for (int i = 0; i < hecImages.Count; i++) //Loop Through All IMG Elements Found

                //Download Image(s) To Specified Path
                wcObj.DownloadFile(hecImages[i].GetAttribute("src"), strPath1 + "\\images\\" + i.ToString() + ".jpg");


We create a webclient object to assist us later in downloading the images. We then create an HTMLElementCollection object, which helps us identify all Image tags on the webpage. If you do not have a basic understanding of HTML, I suggest you have a look here.

We store the Webpage's title and create a folder on C:\ with the same name. Inside this folder we create a subfolder named Images. This is where we will download our pictures to.

The fun part here is the loop. We use a for loop to loop through all of the images found and save them via the DownloadFile method of the WebClient object. We find the image's location through the HTML IMG Tag's SRC attribute. Voila! Quick and simple.


Although this is but a small tool, its possibilities are endless. I am including the Source files just in case you missed something. Until next time, cheers from a lazy Hannes!

Related Articles



  • Not working with me

    Posted by Muhamad on 04/23/2014 01:24am

    wbID is not defined , when I try to define it by : Dim wbID As New WebBrowser it points null reference to the line : Dim hecImages As HtmlElementCollection = wbID.Document.GetElementsByTagName("img") I am using VB.NET 2005 thanks.

    • Not compatible

      Posted by Ezequiel on 10/07/2015 08:17am

      this code only work in VB 2010+ (100% work for me in vb net 2013) greeting!!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date