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");
}
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");
}