Screenshot method generates black images

Msegling

After failing to use the control.drawtobitmap in c#, my second option was to take screenshots of the desktop and crop out the desired sections. My hiccup shows up once i switch user accounts, although the program does not crash, once the user is switched the program generates pure black images only.

I used this code as a reference: WebBrowser.DrawToBitmap() or other methods?

I guess logically this makes sense as this would help windows save resources.

What options/ solutions do i have in my situation?

Edit 1 made a modification to the code for testing:

        int c = 0;
        while (true)
        {
            try
            {
                c++;
                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);
                bmp.Save("picture" + c.ToString() + ".jpg");
                Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

this works perfectly while on the user account, but as soon as i switch users, it returns the exception: The handle is invalid.

Any ideas?

Edit 2:

The bug in DrawToBitmap is not exactly random... if i used the code you supplied:

Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
this.DrawToBitmap(bmp, this.ClientRectangle);
bmp.Save(".\\picture.jpg");

it works perfectly, example: http://oi61.tinypic.com/1z23ynp.jpg

However, the moment i right-click on the web-browser control, DrawToBitmap will return a blank image.

example: http://oi60.tinypic.com/9ay0yc.jpg

So i can easily overcome this bug by adding

((Control)webbrowser1).Enabled = false;

this makes any clicking impossible on the web-browser, but unfortunately to deactivate it would render my project useless as its main function is to emulate mouse clicks on a web-browser control. although this might also be a problem if the window is hidden.

currently im looking at this post, where code is supplied to give you a window handle.

Simulate click into a hidden window it seems it might be of some value... do have a look.

TaW

What were the problems you had with DrawToBitmap? It works fine here, (W8.1, VS2013) even with a Webcontrol and also after switching users. (But see the edit at the end for 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 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);
//Clipboard.SetImage(bmp); for testing only
bmp.Dispose();

I can switch users like I want, the program keeps working.

BTW, the link you posted is really old, many things may have improved.

Edit:

With the updated question things are a lot clearer.

So you want to continuously get a screenshot of your program even when the user has changed, right? and you want to display a WebControl, right?

A user can have three types of desktop: the logon/logoff screen, the screensaver screen and one or more normal desktop screen(s). But while the user is logged off he has no desktop screen at all.

Therefore the screenshot method will not work if the user has no active desktop, neither as g.CopyFromScreen, which will cause a GDI-error nor using a window handle like in the various solutions on the web, including the ones your link leads to. All these will, at best, show a blank or black screen.

So the DrawToBitmap method is the only one that works.

You wrote that it has random errors. That's not what I see.

The problems come in predictably when the user interacts with the WebBrowser in any way. This includes scrolling or clicking with or without navigation. After these interactions the WebBrowser will draw itself as an empty box until its URL is reloaded - not only refreshed - but really reloaded by webBrowser1.Uri = new Uri(uriPath). This can be done, see my other post

The WebBrowser also has another issue when doing a DrawToBitmap: It will fail (with the said empty box) for any pages that include an <input type="text" element. I'm not sure what's the best way to workaround this, let alone why it happends in the first place.. A screenshot method doesn't have that specific problem.

Edit 2:

The code the OP has dug up code which, using a call to PrintWindow, seems to solve all problems we had: It works while being logged off, works with Refeshing even after clicking in the WebBrowser and scrapes all pages, including those with textual input fields. Hoorah!

After cutting down the slack here is a version that can create a copy of the Form or just the 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)
{
    //Bitmap bmp = new Bitmap(ctl.Width, ctl.Height);  // includes borders
    Bitmap bmp = new Bitmap(ctl.ClientRectangle.Width, ctl.ClientRectangle.Height);  // content only
    using (Graphics graphics = Graphics.FromImage(bmp))
    {
        IntPtr hDC = graphics.GetHdc();
        try      { PrintWindow(ctl.Handle, hDC, (uint)0);   }
        finally  { graphics.ReleaseHdc(hDC);                }
    }
    return bmp;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Screenshot Black in Android

From Dev

Getting a Black Screenshot with BabylonJS

From Dev

Black screenshot taken with 'import' (ImageMagick)

From Dev

Imagick::resizeImage generates black image

From Dev

Display black screen while capture screenshot of GLSurfaceView

From Dev

JAVA: Screenshot after JFrame change just black

From Dev

Screenshot robot only captures a black screen on Debian

From Dev

Display black screen while capture screenshot of GLSurfaceView

From Dev

Googlemaps Coded screenshot returns black image

From Dev

Detect black/almost black JPG images in PERL

From Dev

Helper method generates

From Dev

Paperclip (imagemagick) generates dark images

From Dev

Paperclip (imagemagick) generates dark images

From Dev

Take screenshot method inside Fragment

From Dev

Take screenshot method inside Fragment

From Dev

How to identify black and white images in folder of images

From Dev

Black boxes, missing or scaled images

From Dev

Resizing with GD outputs black images

From Dev

opencv printing weird black images

From Dev

Black circles instead of images in Corona

From Dev

PhantomJS returns black screenshot when is started from Jersey rest service

From Dev

Android Take Screenshot of Surface View Shows Black Screen

From Dev

PhantomJS returns black screenshot when is started from Jersey rest service

From Dev

Windows 10 Screenshot Black - Multiple Tools (Standard, Greenshot, Snipping Tool)

From Dev

getattr on a decorated method generates a TypeError

From Dev

HTML2canvas generates Blurry images

From Dev

Upgrading distro generates new and different boot images

From Dev

Sorl-thumbnail generates black square instead of image

From Dev

ffmpeg's select-filter generates unwanted black frames

Related Related

  1. 1

    Screenshot Black in Android

  2. 2

    Getting a Black Screenshot with BabylonJS

  3. 3

    Black screenshot taken with 'import' (ImageMagick)

  4. 4

    Imagick::resizeImage generates black image

  5. 5

    Display black screen while capture screenshot of GLSurfaceView

  6. 6

    JAVA: Screenshot after JFrame change just black

  7. 7

    Screenshot robot only captures a black screen on Debian

  8. 8

    Display black screen while capture screenshot of GLSurfaceView

  9. 9

    Googlemaps Coded screenshot returns black image

  10. 10

    Detect black/almost black JPG images in PERL

  11. 11

    Helper method generates

  12. 12

    Paperclip (imagemagick) generates dark images

  13. 13

    Paperclip (imagemagick) generates dark images

  14. 14

    Take screenshot method inside Fragment

  15. 15

    Take screenshot method inside Fragment

  16. 16

    How to identify black and white images in folder of images

  17. 17

    Black boxes, missing or scaled images

  18. 18

    Resizing with GD outputs black images

  19. 19

    opencv printing weird black images

  20. 20

    Black circles instead of images in Corona

  21. 21

    PhantomJS returns black screenshot when is started from Jersey rest service

  22. 22

    Android Take Screenshot of Surface View Shows Black Screen

  23. 23

    PhantomJS returns black screenshot when is started from Jersey rest service

  24. 24

    Windows 10 Screenshot Black - Multiple Tools (Standard, Greenshot, Snipping Tool)

  25. 25

    getattr on a decorated method generates a TypeError

  26. 26

    HTML2canvas generates Blurry images

  27. 27

    Upgrading distro generates new and different boot images

  28. 28

    Sorl-thumbnail generates black square instead of image

  29. 29

    ffmpeg's select-filter generates unwanted black frames

HotTag

Archive