0

I know that an image can be downloaded from a website opened in web browser control, but that actually works by getting the image URL and download that. There is a website that doesn't allow the direct link, so the image fails to download, so the only method I can think of is copying the images from the cache of the web browser control.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Junaid Rehman
  • 169
  • 4
  • 11

1 Answers1

1

You can use online c# to vb.net converters like telerik or developerfusion to get code in vb.net in the future.

  1. Add reference to Microsoft.mshtml to your project
  2. Add next code into the webBrowser DocumentCompleted event handler

Sample code:

Private Sub webBrowser1_DocumentCompleted(sender As Object, _
   e As WebBrowserDocumentCompletedEventArgs) _
   Handles WebBrowser1.DocumentCompleted

   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
    imgRange.add(DirectCast(img, IHTMLControlElement))

    imgRange.execCommand("Copy", False, Nothing)

    Using bmp As Bitmap = DirectCast( _
        Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
        bmp.Save("C:\" + img.nameProp)
    End Using
   Next

End Sub
volody
  • 6,946
  • 3
  • 42
  • 54
  • why does it say IHTMLDocument2 not defined? I have added the reference to mshtml already.. and same error for IHTMLControlRange and IHTMLImgElement – Junaid Rehman Jun 03 '13 at 07:16
  • ok that issue is resolved, but files not not saved and not even on clipboard – Junaid Rehman Jun 03 '13 at 10:12
  • doc.images collection will enumerate only main document images (top frame), maybe your images are under frames collection – volody Jun 03 '13 at 15:38
  • @user64: How do I do the same thing but to the pictures inside a particular iFrame? – Osprey Jul 22 '13 at 11:27
  • use the Frames property, sample code is in http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlwindow.frames.aspx – volody Jul 22 '13 at 15:14