how to grab all images from webpage

JohnHenderson1963 21 Reputation points
2021-05-07T13:59:37.84+00:00

Hi There,

I wonder if someone can help. First of all this relates to the following link, https://social.msdn.microsoft.com/Forums/en-US/97f32ddf-6bdf-4b3a-a23b-f245434109fb/how-to-grab-all-images-from-webpage?forum=vbgeneral.

The code that I have tried to use was the very last example at the bottom of the page. The issue I have appears at this line of code

img.Save(e.Argument(0) & "\img" & i & ".png")

with the error of "Object reference not set to an instance of an object"

Im not sure as to why I am getting this error. I am using VB 2019 latest community version. Any assistance would be appreciated.

Thank you.

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-05-12T02:57:51.817+00:00

2 additional answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-05-07T16:11:42.977+00:00

    Maybe try this modification:

    If img IsNot Nothing Then img.Save(e.Argument(0) & "\img" & i & ".png")

    and clarify why some of ‘IMAGES(i) = Image.FromStream(_WebStream)’ probably fail.


  2. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-05-10T08:16:05.637+00:00

    Hi @JohnHenderson1963 ,
    You can consider using WebBrowser control to download all images.
    For example:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            WebBrowser1.Navigate("your url")  
        End Sub  
      
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            Dim browser = CType(sender, WebBrowser)  
            Dim client = New WebClient()  
      
            For Each img In browser.Document.Images  
                Dim image = TryCast(img, HtmlElement)  
                Dim src = image.GetAttribute("src").TrimEnd("/"c)  
      
                If Not Uri.IsWellFormedUriString(src, UriKind.Absolute) Then  
                    src = String.Concat(browser.Document.Url.AbsoluteUri, "/", src)  
                End If  
      
                Dim filename = New String(src.Skip(src.LastIndexOf("/"c) + 1).ToArray())  
                If filename.EndsWith(".png") Or filename.EndsWith(".jpg") Then  
                    File.WriteAllBytes(filename, client.DownloadData(src))  
                End If  
      
            Next  
        End Sub  
    

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.