views:

114

answers:

1

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?

+2  A: 

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
You are now officially the hero of the day in our company.
Serhat Özgel
I'm not sure how this answers the question. What was the resolution?
Michael Hedgpeth
@Michael I added a possible resolution to the problem.
Shay Erlichmen