2010-09-01 82 views
0

我試圖自動提交一個表單,並保存TWebBrowser對象中顯示的結果圖像。TWebBrowser - 掛鉤接收事件

圖像被加載在幾個鏈接的JavaScript請求(AJAX),直到最後出現在文檔中。

什麼是獲得此圖像的最佳方式? 我想到掛鉤接收功能能夠看到http響應(這將基本上是我的形象)。

另一種可能是負載從緩存/內存圖像...

我沒有任何想法如何在實踐中做到這一點,我希望有人可以提供幫助。

謝謝。

回答

1

您可以使用IHTMLDocument2對象的images屬性檢索所有的url圖像。

使用OnDocumentComplete事件查看此示例。

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; 
+0

謝謝你,這工作。任何想法如何我可以通過TWebBrowser下載圖像(我需要相同的會話數據/ cookie,所以我不能使用urlmon/indy/...) – maxedmelon 2010-09-02 00:45:06

1

可以使用OnDocumentCompleteOnNavigateComplete2事件(見SHDOCVW幫助)或等待WebBrowser是在一個ReadyState READYSTATE_COMPLETE,然後從WebBrowser.Document讀取。

但是你也可以(更簡單的IMO)使用TIdHTTP.Get直接得到響應流。

+0

我需要知道TidHTTP.Get的確切圖像url,這並不那麼容易。 此外,我無法從文檔中讀取圖像,因爲它不是標記,而是通過AJAX加載到容器中。 – maxedmelon 2010-09-01 23:14:09

+0

「我無法從文檔中讀取圖像」...不正確。您可以使用圖像集合,並且可以訪問文檔中的其他所有內容。 – 2010-09-02 01:28:38

+0

是的,對不起。我現在開始工作了。你是對的:) – maxedmelon 2010-09-02 10:15:56

1

爲了與您的應用程序更具可擴展性,你可以直接嘗試EmbeddedWB。它包裝IWebBrowser2,非常方便使用。 Embarcadero在其RADStudio中使用EmbeddedWB。

+0

將得到新的RAD Studio並試一試。我也看到了一些TIEDownload對象。如果我很幸運,它會使用與TWebBrowser對象相同的會話數據下載文件... – maxedmelon 2010-09-02 10:34:13