0

I used the code below to successfully get a copy of each picture inside a page loaded using webbrowser control.

    Dim doc As IHTMLDocument2 = DirectCast(wb.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(img.nameProp)
  End Using
Next

I got the code from here: Copy an image from cache of web browser control present in VB.NET

However, the picture I am interested in is inside an iFrame.

I tried changing:

Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)

to

Dim doc As IHTMLDocument2 = DirectCast(wb.Document.Window.Frames(iFrameID).Document.DomDocument, IHTMLDocument2)

but I am getting an "Access Denied" Error. I guess (not sure) its because the iframe's src is on a different domain.

Is there a way around this problem?

Thanks!

Community
  • 1
  • 1
Osprey
  • 1,523
  • 8
  • 27
  • 44

1 Answers1

0

If the iFrame has content from a domain that is different from the the parent then your out of luck. You could attempt a different solution and retrieve the iFrame's page via Ajax and parse out the image src. Another alternative is to do it server side with a program like PhantomJs (or VB, PHP etc) to retrieve the page and parse it for images to retrieve.

BigBadOwl
  • 669
  • 2
  • 9
  • 22
  • Is it possible to determine the URL of the page that is loaded in the iFrame? – Osprey Jul 23 '13 at 07:24
  • You can get the "source" of the the iFrame using document.getElementById("theiframe").src, but if the iFrames navigation has changed then it won't work. see http://stackoverflow.com/questions/44359/how-do-i-get-the-current-location-of-an-iframe. – BigBadOwl Jul 23 '13 at 07:35
  • That's the first thing I had tried but I keep getting an error reading "'src' is not a member of 'System.Windows.Forms.HtmlElement'." I'm not too worried about the contents of the iframe changing since there is a delay. When in the browser, right clicking on the picture in the iFrame allows me to copy the picture I am interested in. Do you know if I can emulate that in webbrowser control programmatically? – Osprey Jul 23 '13 at 08:01
  • The reference is to an HtmlWindow object (Dim cf1Frame As HtmlWindow = WebBrowser1.Document.Window.Frames("theiframe")) so the Url property should give you the url. – BigBadOwl Jul 23 '13 at 08:41
  • Now I get "'System.UnauthorizedAccessException' occurred in System.Windows.Forms.dll" :( – Osprey Jul 23 '13 at 11:54