6

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

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

It looks like they did have:

webBrowser1.DrawToBitmap(); // but its unsupported and doesnt work
  1. 3rd party api - not wasting my time
  2. .DrawToBitmap and nonanswer-links
  3. 100 wrong answers
  4. just takes a screenshot
McMillan Cheng
  • 382
  • 1
  • 6
  • 20
user1873073
  • 3,580
  • 5
  • 46
  • 81

2 Answers2

6

Try to make sure you are calling the method in the DocumentCompleted event.

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));
John Saunders
  • 160,644
  • 26
  • 247
  • 397
scartag
  • 17,548
  • 3
  • 48
  • 52
  • that may be it but my goal is to be able to do a google image search and click show more until it gets to the bottom and then click a button so documentcompleted won't work. – user1873073 Jan 05 '13 at 01:47
  • @user1873073 not sure why you think it won't work. document completed will be called multiple times (one for each new request) what matters is what it does at the last call. – scartag Jan 05 '13 at 01:48
  • Ya know, I originally wrote this huge implementation that took a picture directly from windows of the open control, then scrolled the control, and stitched the images together. Then I fixed as many scroll related issues as possible... But just now I randomly found your post, and went wide-eyed: you can just set the width to be far larger than what can display on the screen, and there's no issue?! Your two little lines there replaced some 200-300 in my program. The only difference is I'm now using an activeX capturer, as DrawToBitmap is unreliable. Anyway, thank you for simplifying my code. – Robert Christ Apr 15 '14 at 17:23
3

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:

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

but its unsupported and doesnt work

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).

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();
     }
}
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • 2
    Do you have any work around for the issue that you have faced "The blank image Issue". Because i have just faced it. Thanks – Vivekh Aug 13 '14 at 07:45
  • I am having svg image as background and html on top of it and data from json is mapped to dom inside webbrowser control. I am getting blank image as well – Sandeep May 25 '16 at 21:09
  • @Sandeep As I said in the answer, I don't know how to resolve the blank page issue if your html page is quite complex. – Cheng Chen May 26 '16 at 02:02