Taking a Screenshot of a WebBrowser on a Tab

J

jwgoerlich

Hello group,

I have a project with several WebBrowser objects on separate tabs. I
want to take screenshots of these. I am currently using the
PrintWindow function. (Code below.) This works if the tab is open and
the WebBrowser is displayed. For browsers on other tabs, however, the
resulting bitmap is black.

Any ideas on how to tweak this code so that it snapshots WebBrowsers
on tabs that are not selected? Or should I be using a different method
altogether?

Thank you,

J Wolfgang Goerlich


The C# code ...

[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt,
uint nFlags);

private void BrowserSnapShot(System.Windows.Forms.WebBrowser
targetBrowser)
{
// Get the image width and height

int screenWidth = targetBrowser.Width;
int screenHeight = targetBrowser.Height;

// Create the output

// HWND hwnd, Window to copy
// HDC hdcBlt, HDC to print into
// UINT nFlags, Optional flags

// Set hwnd to the WebBrowser

IntPtr myIntptr = targetBrowser.Handle;
int hwndInt = myIntptr.ToInt32();
IntPtr hwnd = myIntptr;

// Set hdc to the bitmap

Bitmap bm = new Bitmap(screenWidth, screenHeight,
System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics g = Graphics.FromImage(bm);
IntPtr hdc = g.GetHdc();

// Snapshot the WebBrowser

bool result = PrintWindow(hwnd, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();

// Save the bitmap, if successful

if (result == true)
bm.Save("C:\\Inetpub\\wwwroot\\slide.bmp");

}
 
A

Alexey Smirnov

Hello group,

I have a project with several WebBrowser objects on separate tabs. I
want to take screenshots of these. I am currently using the
PrintWindow function. (Code below.) This works if the tab is open and
the WebBrowser is displayed. For browsers on other tabs, however, the
resulting bitmap is black.

Any ideas on how to tweak this code so that it snapshots WebBrowsers
on tabs that are not selected? Or should I be using a different method
altogether?

Did you try to use WebBrowser.DrawToBitmap()?

As far as I know it does the same and you can use it for all your
WebBrowser objects in the same time
 
J

jwgoerlich

Did you try to use WebBrowser.DrawToBitmap()?

Is there something special needed to enable this? Visual Studio 2005
reports that "this method is not supported by this control."

J Wolfgang Goerlich
 
J

jwgoerlich

F1 :)

Good ol' help. Though not supported, it does work just as detailed in
the ASP.NET article (thanks!).

"DrawToBitmap - This method is not supported by this control.
(inherited from WebBrowserBase)"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser_members.aspx

And yet, the DrawToBitmap acts the same as the PrintWindow. It creates
a good snapshot if the tab with the WebBrowser is selected. It creates
a black, blank bitmap if the tab is not selected. Looks like I may
have to settle for selecting the tab with the WebBrowser, creating the
bitmap, and then going back to the originally selected tab.

J Wolfgang Goerlich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top