![](/img/trans.png)
[英]Take screenshot: WinForms application with Web Browser Control in Form in Windows Server
[英]Get a screenshot of the web browser control?
關於這一點有很多線索,但沒有一個是明確的,沒有我嘗試過甚至工作正常。
獲取整個Web瀏覽器控件內容的代碼是什么(即使是屏幕外的內容)?
看起來他們確實有:
webBrowser1.DrawToBitmap(); // but its unsupported and doesnt work
嘗試確保在DocumentCompleted
事件中調用方法。
webBrowser1.Width = wb.Document.Body.ScrollRectangle.Width;
webBrowser1.Height = wb.Document.Body.ScrollRectangle.Height;
Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height);
webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser1.Width, webBrowser1.Height));
我上周在我的項目中正在開發一個類似的功能,閱讀了一些關於這個主題的帖子,包括你的鏈接。 我想分享一下我的經歷:
該函數的關鍵部分是System.Windows.Forms.WebBrowser.DrawToBitmap方法。
但它沒有支持,也沒有工作
它受支持並且確實有效,但並不總是正常工作。 在某些情況下,你會得到一個空白圖像截圖(根據我的經驗,它加載的更復雜的html,它失敗的可能性越大。在我的項目中,只有非常簡單和格式良好的htmls將加載到WebBrowser控件中,所以我永遠不會得到空白圖像)。
無論如何,我也沒有100%完美的解決方案。 這是我的核心代碼的一部分,希望它有所幫助(它適用於ASP.NET MVC 3)。
using (var browser = new System.Windows.Forms.WebBrowser())
{
browser.DocumentCompleted += delegate
{
using (var pic = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(pic, new Rectangle(0, 0, pic.Width, pic.Height));
pic.Save(imagePath);
}
};
browser.Navigate(Server.MapPath("~") + htmlPath); //a file or a url
browser.ScrollBarsEnabled = false;
while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.