WebBrowser.DrawtoBitmap() generating blank images for few sites consistently

I’ve been using WebBrowser.DrawtoBitmap() in my asp.net page running in separate STA thread to capture web pages as an image. But I found that I’m getting blank images for few sites consistently. I’m aware that the method is not ‘officially’ supported but it would be nice if someone can provide me any reason or a work around for these blank images issue.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

DrawToBitmap has limitations and dont always work as expected. Try instead work with native GDI+

Here is example

Method 2

This problem can be solved by giving focus to the control, so it’ll draw properly.

This’d be an option, but the control would have to be visible: WebBrowser.DrawToBitmap() or other methods?

As far as I’ve heard about the problem is that its fixed when you click the webbrowsercontrol. Therefore doing this programmatic should solve the problem 🙂

I haven’t tested this, but in theory I think its possible to launch a windows form in an asp.net application.
Reference to system.windows.forms and to drawing, then use application.run on a separate thread.
Note: I’m on my phone so I can’t test it, but it might actually work.

Method 3

You’re not hitting pages with Flash?

I had to do this, in order to get my WebBrowser control to work:

using System;
using System.Windows.Forms;

public class WebBrowserEx : WebBrowser
{
   public WebBrowserEx()
   {
   }

   protected override void WndProc(ref Message m)
   {
      switch (m.Msg)
      {
         case 0x021:
         case 0x201:
         case 0x204:
         case 0x207:
             base.DefWndProc(ref m);
             return;
      }
      base.WndProc(ref m);
   }
}

I seriously have no idea what I was doing, I just found it somewhere and it was to enable Flash. But I think I haven’t had to much problems with it since.

I use it to do screenshots. I run it on a separate STA thread (as a windows service – nothing visible).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x