1. 目的
在Codeproject 中看到有人做這個, 稍微做了下修改, 做成了命令行的, 使用方法是
iesnap.exe url filename
用以抓取 URL 對于的Web, 并且將Web 的截圖保存在Filename 中.
url : 要截圖的網址
filename: 截圖保存文件名
2. 思路
主要使用WebBrowser Control 和 MSHTML 來完成.
a. 創建WebBrowser control
b. 從WebBrowser Control 獲取 IWebBrowser2 接口, 用 IWebBrowser2::Navigate2 來訪問URL
c. 獲得IHTMLElementRender (WebBrowser -> IHTMLDocument2 -> IHTMLElement -> IHTMLElementRender), 使用該接口的DrawToDC 來獲取URL 對應的頁面截圖內容.
d. 用GDI 的 Image 來保存截圖到Filename
3.
可以看看原文以得到更好的解答
我看的那個文章: Capture an HTML document as an image
另外一篇C# 的文章: Image Capture Whole Web Page using C#
4. 問題
a. DocumentComplete 事件會在URL 對應頁面的每個元素下載完畢的時候都會發生, 所以:( 還不知道哪個事件是整個頁面下載完畢的事件:/
b. 現在是通過MFC 來創建WebBrowser Control 的, 不知道怎么在Win32 Application 中創建這個控件.
c. 不知道如何得到整個頁面的大小, 現在只能保存頁面截圖的一部分:(
這個已經知道怎么做了.
IHTMLElement2 的 scrollWidth & scrollHeight 加上 scrollLeft & scrollTop 來實現, 但是不知道為什么, put_scrollTop & put_scrollLeft 這些函數好像不對, 只能截獲到一部分頁面截圖.:( 不知道為什么
代碼可以在這里下載: http://www.shnenglu.com/Files/bigsml/iesnapshot.zip