I have this code:
I'm trying to have my program download all the images (that contains "jpg") from a site.

Dim doc As IHTMLDocument2 = DirectCast(WebBrowser1.Document.DomDocument, IHTMLDocument2)
Dim imgRange As IHTMLControlRange = DirectCast(DirectCast(doc.body, HTMLBody).createControlRange(), IHTMLControlRange)

        For Each img As IHTMLImgElement In doc.images
            If img.nameProp.Contains("jpg") Then
                imgRange.add(DirectCast(img, IHTMLControlElement))
                imgRange.execCommand("Copy", False, Nothing)
                Using bmp As Bitmap = DirectCast(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
                    'bmp.Save("test.gif", System.Drawing.Imaging.ImageFormat.Gif)
                    bmp.Save("MyImages")
                End Using
            End If
        Next
        WebBrowser1.Navigate("http://www.yahoo.com")

Getting an error on line 1 with System.NullReferenceException (Object reference not set to an instance of an object).

1. Add reference to Microsoft HTML Object Library when working with DomDocument .
.File menu, Project/Add Reference.../COM tab/Microsoft HTML Object Library.

2. Add an import to the top of your Form for mshtml .

Imports mshtml '// After adding the reference, Import mshtml.
Public Class Form1

Oh, I forgot to say I already did that and imported mshtml. That was my mistake, sorry.
Still getting the same error even adding the reference and importing mshtml..

Then your code is not in the Private Sub WebBrowser1_DocumentCompleted(.. event,
and your WebBrowser does not have a page loaded.

I would also save the images under the proper ImageFormat.

Static i As Integer = 1
                    bmp.Save("C:\TEMP\test" & i & ".jpg", Imaging.ImageFormat.Jpeg)
                    i += 1

Thank you very much for the reply.
But how do I make it so when I click the a button on my form, the webbrowser navigates to a site and download/save the images?

EDIT: Disregard the above question.
But now when I load the site and the program tries to download the images, I get the system.nullreferenceexception..but I can see that one image did however save on the location I specified it to.

Can't edit my previous post so..
It seems the last error i was getting was just the debugging. It works fine when run as exe.
But now I want to add two things to it.
One is that how do I modify the Contains("jpg") so that if a site contains png, gif, jpg, etc. it will still download all of those rather than just the jpg ones.
Second is that how do I find out if the program has finished saving all the images on that page so that I can have a msgbox saying it has finished?

See if this helps.

Imports mshtml
Public Class Form1
    Private bDownloadImages As Boolean = False '// determine if to download images or not.
    Private myImagesFolder As String = "C:\TEMP\" '// folder for images to be saved to.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        bDownloadImages = True '// set to True to only download images when needed.
        WebBrowser1.Navigate("http://www.yahoo.com/") '// load page in WebBrowser.
    End Sub

    '// when WebBrowser finishes loading the page...
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If bDownloadImages = True Then
            getImages() '// Download Images.
            Application.DoEvents() '// give it time to finish downloading images.
            bDownloadImages = False '// set back to False.
        End If
    End Sub

    Private Sub getImages()
        Dim doc As IHTMLDocument2 = DirectCast(WebBrowser1.Document.DomDocument, IHTMLDocument2)
        Dim imgRange As IHTMLControlRange = DirectCast(DirectCast(doc.body, HTMLBody).createControlRange(), IHTMLControlRange)
        Dim myImagesFileExtenstions() As String = {".png", ".jpg", ".gif"} '// String Array containing your image file .Extensions.
        Dim sImageExtension As String = Nothing '// get file extension from file.
        For Each img As IHTMLImgElement In doc.images
            For Each imgToFind As String In myImagesFileExtenstions '// loop thru all image file .Extensions.
                If img.nameProp.EndsWith(imgToFind) Then '// if it contains,...
                    imgRange.add(DirectCast(img, IHTMLControlElement))
                    imgRange.execCommand("Copy", False, Nothing)
                    Using bmp As Bitmap = DirectCast(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
                        Static i As Integer = 1
                        sImageExtension = img.nameProp.Substring(img.nameProp.LastIndexOf(".")) '// get file .Extension of image.
                        Select Case sImageExtension '// save image files with proper ImageFormat.
                            Case ".png"
                                bmp.Save(myImagesFolder & "\test" & i & sImageExtension, Imaging.ImageFormat.Png)
                            Case ".jpg"
                                bmp.Save(myImagesFolder & "\test" & i & sImageExtension, Imaging.ImageFormat.Jpeg)
                            Case ".gif"
                                bmp.Save(myImagesFolder & "\test" & i & sImageExtension, Imaging.ImageFormat.Gif)
                            Case Else
                                bmp.Save(myImagesFolder & "\test" & i & sImageExtension) '// ???, just save? :D
                        End Select
                        i += 1
                    End Using
                End If
            Next
        Next
    End Sub
End Class

The above code should navigate to a website, give it time to load the page, and will download the images only when clicking the Button.
I also added a Select Case to determine which format to save the images with, but it does not seem to save as it should.
.A .gif image should animate after being downloaded, which does not with the code you have provided.

I would use:

My.Computer.Network.DownloadFile("http://imageUrlWith.ExtensionHere", "C:\My Folder on my p.c. and image.ExtensionHere")

...to download files, which should download them as they are, even animated .gifs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.