What were the problems with DrawToBitmap? It works great here (W8.1, VS2013) even with Webcontrol, and also after switching users. (But see Editing at the end of the conditions!)
Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); this.DrawToBitmap(bmp, this.ClientRectangle); // Clipboard.SetImage(bmp); for testing only bmp.Dispose();
Here is the code to take a screenshot of your window:
Rectangle formBounds = this.Bounds; Bitmap bmp = new Bitmap(formBounds.Width, formBounds.Height ); using (Graphics g = Graphics.FromImage(bmp)) g.CopyFromScreen(formBounds.Location, Point.Empty, formBounds.Size);
I can switch users as I want, the program continues to work.
By the way, the link you posted is really old, many things can be improved.
Edit:
With the updated question, everything is much clearer.
So, you want to constantly get a screenshot of your program, even when the user has changed, right? and you want to display WebControl, right?
A user can have three types of desktop: an input / output screen, a splash screen, and one or more regular desktop screens. But while the user is logging out, he does not have a desktop screen at all.
Therefore, the screenshot method will not work if the user does not have an active desktop, and not like g.CopyFromScreen , which will cause a GDI error or use a window handle, as in various solutions on the Internet, including those related to your link leads to. All this, at best, will show a blank or black screen.
So the DrawToBitmap method is the only one that works.
You wrote that he has random errors. This is not what I see.
Problems arise in a predictable way when a user interacts with WebBrowser in any way. This includes scrolling or clicking with or without navigation. After these interactions, WebBrowser will draw itself as an empty box until its URL is reloaded - not only updated, but webBrowser1.Uri = new Uri(uriPath) will also be reloaded. This can be done, see my other post.
WebBrowser also has another problem when executing DrawToBitmap : it will fail (with the specified blank field) for any pages containing the <input type="text" element. I'm not sure how best to solve this, not to mention why this happens in the first place. The screenshot method does not have this particular problem.
Edit 2:
The OP code dug up a code that, using the PrintWindow call, seems to solve all the problems that we had: it works when you log out, works with Refeshing even after clicking in WebBrowser and resets all pages, including text input fields. Hoorah!
After reducing sagging, there is a version that can create a copy of Form or just WebBroser (or any other Control ) with or without borders:
[DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags); public Bitmap CaptureWindow(Control ctl) {