Hi 
I'm trying to capture screenshots of panels, containing webbrowsers.
Surprisingly after all that I read on the Internet, DrawToBitmap does work in most cases. However some certain pages in the webbrowser render all white.
I have a small method to check if the screenshot is all white, and if so, to use an alternate method to capture the image. This "CheckAllWhite" method works fine, however when I try to use the alternate capture method, it always works the second time.
Say for example, I have two panels with one webbrowser each, one viewing "google.com", one viewing "youtube.com". Those both are sites that render white with the original DrawToBitmap.
The alternate method only works if the control is topmost and currently visible. So when I find that I have to re-capture, I bring the panel that I want to capture to front, and then try DrawToImage.
Here's the code for that: ("t" is the panel)
And here is the code of the DrawToImage itself: (found from the net and fixed a little to give square image)
The problem is - if I try to capture the panel that is already top-most, it works.
If I try to capture a background panel, it captures the other one, despite the fact that I call .BringToFront() before the capture.
When I try again to capture the same panel (the one that was originally background, but that I brought to front with the last call), after it already is top-most, it works.
So generally it acts as if I call .BringToFront() at the end of the method.
Any clue how to fix that? Hackish approaches are acceptable
Edited by ronnin426850 - 11/11/12 at 7:04am

I'm trying to capture screenshots of panels, containing webbrowsers.
Surprisingly after all that I read on the Internet, DrawToBitmap does work in most cases. However some certain pages in the webbrowser render all white.
I have a small method to check if the screenshot is all white, and if so, to use an alternate method to capture the image. This "CheckAllWhite" method works fine, however when I try to use the alternate capture method, it always works the second time.
Say for example, I have two panels with one webbrowser each, one viewing "google.com", one viewing "youtube.com". Those both are sites that render white with the original DrawToBitmap.
The alternate method only works if the control is topmost and currently visible. So when I find that I have to re-capture, I bring the panel that I want to capture to front, and then try DrawToImage.
Here's the code for that: ("t" is the panel)
Code:
Bitmap b = new Bitmap(size, size);
t.DrawToBitmap(b, t.ClientRectangle);
if (Utls.CheckAllWhite(b))
{
t.BringToFront();
b = (Bitmap)Utls.DrawToImage(t);
}
panelThumb.BackgroundImage = b;
And here is the code of the DrawToImage itself: (found from the net and fixed a little to give square image)
Code:
public static Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = GetWindowDC(handle);
RECT windowRect = new RECT();
GetWindowRect(handle, windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
int size = Math.Min(width, height);
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, size, size);
IntPtr hOld = SelectObject(hdcDest, hBitmap);
BitBlt(hdcDest, 0, 0, size, size, hdcSrc, 0, 0, 13369376);
SelectObject(hdcDest, hOld);
DeleteDC(hdcDest);
ReleaseDC(handle, hdcSrc);
Image image = Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
return image;
}
public static Image DrawToImage(this Control control)
{
return CaptureWindow(control.Handle);
}
The problem is - if I try to capture the panel that is already top-most, it works.
If I try to capture a background panel, it captures the other one, despite the fact that I call .BringToFront() before the capture.
When I try again to capture the same panel (the one that was originally background, but that I brought to front with the last call), after it already is top-most, it works.
So generally it acts as if I call .BringToFront() at the end of the method.
Any clue how to fix that? Hackish approaches are acceptable

Edited by ronnin426850 - 11/11/12 at 7:04am





