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.
-
A related q/a: [How to fix a opacity bug with DrawToBitmap on WebBrowser Control?](http://stackoverflow.com/q/21697048/1768303) – noseratio Feb 18 '14 at 08:09
3 Answers
DrawToBitmap
has limitations and dont always work as expected. Try instead work with native GDI+
Here is example
-
This method only works if the `WebBrowser` control is actually visible (on a form I take it?). – Mikael Östberg Mar 18 '11 at 12:21
-
-
1@Aidiakapi i dont take your link, i took it from my answer in another question http://stackoverflow.com/questions/5331658/draw-a-bitmap-from-a-control-taller-than-the-screen/5331987#5331987 – Stecya Mar 18 '11 at 12:28
-
Ok, well me too. But a different question. @NLV check out what I added to my answer. – Aidiakapi Mar 18 '11 at 12:32
-
In case you've run into this recently [Microsoft introduced a breaking change](https://social.msdn.microsoft.com/Forums/en-US/9d690398-1f91-4fbd-82fa-4b663c3b558f/kb3057839-has-broken-windows-forms-controldrawtobitmap-when-called-from-application-launched-from?forum=windowsgeneraldevelopmentissues) in June of 2015 (KB3057839) that was fixed in July 2015 (KB3070102). – AlG Jul 21 '15 at 17:16
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.
-
How can I click the WebBrowser control programmatically? Will Focus() work? – NLV Mar 18 '11 at 12:36
-
-
-
-
@NVL My pc is in use, but this site asks the same: http://pcreview.co.uk/forums/webbrowser-control-simulate-click-t3614740.html – Aidiakapi Mar 18 '11 at 12:46
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).

- 16,982
- 6
- 61
- 79
-
Tried it. Had no effect. http://yahoo.com always being captured as a blank image. – NLV Mar 19 '11 at 05:22
-
I tried out my code now and I cannot get an image of yahoo either. I do have a theory about what's going on though. When I visit "yahoo.com" I first get a 301 which takes me to "www.yahoo.com" where I get a 302. I am then redirected to "se.yahoo.com" (I'm in Sweden) which is the first 200 I get. I think the WebBrowser cannot handle the redirects. – Mikael Östberg Mar 19 '11 at 07:49
-
-
1