获取 Web 浏览器控件的屏幕截图? [英] Get a screenshot of the web browser control?

查看:40
本文介绍了获取 Web 浏览器控件的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于这个的线索,但没有一个是清楚的,而且我尝试过的没有一个实际上工作正常.

There are a lot threads about this but none of them were clear and none I tried actually even worked right.

获取整个 Web 浏览器控件的内容(甚至是屏幕外的内容)的代码是什么?

What is the code to get the contents of the entire web browser control (even that which is off screen)?

看起来他们确实有:

webBrowser1.DrawToBitmap(); // but its unsupported and doesnt work

  1. 第 3 方 api - 不要浪费我的时间
  2. .DrawToBitmap 和 nonanswer-链接
  3. 100 个错误答案
  4. 只需截图

推荐答案

上周我正在我的项目中处理一个类似的功能,阅读了一些关于此主题的帖子,包括您的链接.我想分享我的经验:

I was working on a similiar function in my project last week, read a few posts on this topic including your links. I'd like to share my experience:

这个函数的关键部分是 System.Windows.Forms.WebBrowser.DrawToBitmap 方法.

The key part of this function is System.Windows.Forms.WebBrowser.DrawToBitmap method.

但它不受支持且不起作用

but its unsupported and doesnt work

它受支持并且可以正常工作,但并不总是正常工作.在某些情况下,您会得到一个空白图像屏幕截图(根据我的经验,加载的 html 越复杂,失败的可能性就越大.在我的项目中,只有非常简单且格式良好的 html 会加载到 WebBrowser 控件中,所以我永远不会得到空白图像).

It is supported and does work, but not always works fine. In some circumstances you will get a blank image screenshot(in my experience, the more complex html it loads, the more possible it fails. In my project only very simple and well-formatted htmls will be loaded into the WebBrowser control so I never get blank images).

无论如何,我也没有 100% 完美的解决方案.这是我的核心代码的一部分,希望它有所帮助(它适用于 ASP.NET MVC 3).

Anyway I have no 100% perfect solution either. Here is part of my core code and hope it helps (it works on 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();
     }
}

这篇关于获取 Web 浏览器控件的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆