2

I have a windows service running on a client machine. I need to capture the screen of the client and sent it to server over the remoting. When I run the exe file, it can capture the screen and send it to the server fine. But when I run it as a service, it logs the following error:

"The handle is invalid."

The service's "interact with desktop" checkbox is checked. The code I am using for screenshot is:

Image bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics  gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
return bmpScreenshot;

What may be the cause and how can I resolve this?

Serhat Ozgel
  • 23,496
  • 29
  • 102
  • 138

1 Answers1

5

Services run in session 0 (as of Vista) while the rest of the application don't.

You need to run the screen capture application in the user session after log in using (for example) the Run registry key.

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87