views:

65

answers:

3

Hello, I am trying to automatically submit a form, and save the resulting image that gets shown in the TWebBrowser object.

The image gets loaded over several chained javascript requests (ajax), until it finally appears in the document.

What is the best way to get this image? I thought of hooking the receive function to be able to see the http response (which would basically be my image).

Another possibility could be to load the image from cache / memory...

I don't have any idea how to practically do this, I hope someone can help.

Thanks.

+1  A: 

You can use the OnDocumentComplete or OnNavigateComplete2 events (see the SHDocVw help) or wait for the WebBrowser to be in a ReadyState READYSTATE_COMPLETE and then read from the WebBrowser.Document.

But you can also (simpler IMO) use a TIdHTTP.Get to directly get the response stream.

François
I need to know the exact image url for the TidHTTP.Get, which is not that easy. Also I can not read the image from the Document, because it is no <img> tag, but loaded into a container via AJAX.
maxedmelon
" I can not read the image from the Document"... Not true. You can use the images collection and you have access to everything else that is inside the document.
François
yes, sorry. i made it work now. you were right :)
maxedmelon
+1  A: 

you can retrieve all the url images using the images property from the IHTMLDocument2 object.

see this sample using the OnDocumentComplete Event.

procedure TForm2.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var
HTMLDocument2: IHTMLDocument2;
i            : Integer;
Item         : IHTMLElement;
ImageUrl     : string;
begin
    HTMLDocument2 := (WebBrowser1.Document AS IHTMLDocument2);
    for i := 0 to HTMLDocument2.images.length -1 do
    begin
    Item := HTMLDocument2.images.item(i, null) As IHTMLElement;
    ImageUrl:=item.getAttribute('src',0);
     //do your stuff with the url image retrieved
    end;
end;
RRUZ
Thank you, this worked. Any idea how I can download an image via TWebBrowser (I need the same session data / cookies, so I can't use urlmon/indy/...)
maxedmelon
+1  A: 

To be more scalable with your application you can directly try EmbeddedWB. It wraps IWebBrowser2 and very handy to use. Embarcadero use EmbeddedWB in their RADStudio.

TaveL
Will get the new RAD Studio and give it a try. I saw some TIEDownload object too. If I am lucky, it downloads a file using the same session data as the TWebBrowser object ...
maxedmelon