1

Running on Windows 7 x64, I have the following code which is working fine when i run it as a stand alone application (such as ConsoleApplication1):

var screen = Screen.PrimaryScreen;

using (var bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height))
using (var graphics = Graphics.FromImage(bitmap))
{
    graphics.CopyFromScreen(new Point(screen.Bounds.Left, screen.Bounds.Top), new Point(0, 0),
    screen.Bounds.Size);
    bitmap.Save(@"c:\\Test.png", ImageFormat.Png);
}

I need to put this code in a "Windows Service" with "Local System Account" and "Allow Service to Interact with Desktop", but when I run it there, it is capturing a black screen.

I understand that in Win7 services are isolated and there is no interaction with desktop, but I need to take screenshot somehow, or pass some admin credentials.

The Windows Service MUST run as "Local System Account" and I cannot change it to run as administrator

How can I still take screenshot?

user829174
  • 6,132
  • 24
  • 75
  • 125

2 Answers2

1

Taking a screenshot is related to GUI, but Windows service can't have GUI. This msdn article gives a work around, however it says:Services running in an elevated security context, such as the LocalSystem account, should not create a window on the interactive desktop because any other application that is running on the interactive desktop can interact with this window. This exposes the service to any application that a logged-on user executes. Also, services that are running as LocalSystem should not access the interactive desktop by calling the OpenWindowStation or GetThreadDesktop function.

Also, these posts discussion more this problem: 1. windows service screen capture returns black screen 2. How can I run a Windows GUI application on as a service?

Community
  • 1
  • 1
Matt
  • 6,010
  • 25
  • 36
0

The screen shot you are taking is of the session 0 desktop. This is where services run. The 'allow service to interact with the desktop' option can be a bit misleading by its description alone. Here is a good description of what it allows: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx

There can be many 'desktops' available at any one time, since you can have more than one user logged on (fast user switching and RDP). You may want to refine your approach to either get the 'console' user's session, or the currently logged on user's session.

You are going to need a broker process running in the window session to do this.

The two things I would suggest investigating further and finding the right option for you:

  1. Use CreateProcessAsUser to spawn the process into the user's session to take the screen shot
  2. Have a system tray app running in each user's session that you can interact with (ie named pipes) and tell it to take the screen shot.
Ryan Newington
  • 199
  • 2
  • 8