Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Get Images, Links, and Website Source (Browser Extras)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
16 Jul 2013CPOL 18.7K   770   4   2
Get images, links, and source code from a website.

Introduction

This will show you how to get images, links and source code from a website using a web-browser control. This can be added to an external web-browser project.

Using the Code

The code requires WebBrowser, TabControl, Richtextbox, PictureBox, (2x)ListBox.

Double click the WebBrowser control and in the DocumentCompleted Sub, add the following code:

VB.NET
'
RichTextBox.Text = WebBrowser.DocumentText.ToString 'Gets the source of the current website loaded
'
For Each ele As HtmlElement In WebBrowser.Document.Links
    Dim eletarget As String = ele.GetAttribute("href")
    LINKS-ListBox.Items.Add(eletarget) 'Adds the Links to the ListBox
Next
'
For Each ele As HtmlElement In WebBrowser.Document.All
    '
    If ele.GetAttribute("src").ToLower.Contains(".jpg") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        IMAGES-ListBox.Items.Add(imgsrc) 'Adds all .jpg images to the ListBox
    End If
    '
    If ele.GetAttribute("src").ToLower.Contains(".png") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        IMAGES-ListBox.Items.Add(imgsrc) 'Adds all .png images to the ListBox
    End If
    '
    If ele.GetAttribute("src").ToLower.Contains(".gif") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        IMAGES-ListBox.Items.Add(imgsrc) 'Adds all .gif images to the ListBox
    End If
    '
    If ele.GetAttribute("src").ToLower.Contains(".bmp") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        IMAGES-ListBox.Items.Add(imgsrc) 'Adds all .bmp images to the ListBox
    End If
    '
Next

Now double click the ListBox which will get the images and in the Click Sub, add the following code:

VB.NET
'
PREVIEW-PictureBox.ImageLocation = IMAGES-ListBox.SelectedItem.ToString
'Gets the selected image in the ListBox and previews it in the PictureBox 

Go to the DoubleClick Sub of the same ListBox as before and insert the following code:

VB.NET
'
WebBrowser.Navigate(IMAGES-ListBox.SelectedItem.ToString)
'Selected Image in ListBox navigates to the Image URL in the WebBrowser
'  

Finally, double click the ListBox which will get the links and in the DoubleClick Sub, add the following code:

VB.NET
'
WebBrowser.Navigate(LINKS-ListBox.SelectedItem.ToString)
'Selected Link in ListBox navigates to the link in the WebBrowser
'  

Points of Interest

As you can see, the code is very simple and very easy to build onto. It doesn't have to be links or images, it can also be other files and resources.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi25-Jul-13 20:53
professionalAmir Mohammad Nasrollahi25-Jul-13 20:53 
GeneralMy vote of 5 Pin
_Vitor Garcia_17-Jul-13 6:35
_Vitor Garcia_17-Jul-13 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.