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
3
  • 3
    If you mean SO threads, how about linking to them and specifying why each of them didn't work Jan 5, 2013 at 1:37
  • there you are Matt. btw do you know the answer? Jan 5, 2013 at 1:46
  • @user1873073 please post part of your code. specifically where u are loading the doc and your document completed event.
    – scartag
    Jan 5, 2013 at 1:47

2 Answers 2

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));
3
  • 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. Jan 5, 2013 at 1: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 5, 2013 at 1: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. Apr 15, 2014 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();
     }
}
3
  • 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, 2014 at 7: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, 2016 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, 2016 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.