0

I am building a screenshoting service. I need to be able to: 1) Start a process (MSWord.exe) 2) SendKeys to the process 3) Take a screenshot

while (true)
{
    Rectangle bounds = Screen.PrimaryScreen.Bounds;
    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        }
        bitmap.Save(string.Format("result{0}.png", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")),
                    ImageFormat.Png);
    }
    Thread.Sleep(10000);
}

Everything works fine if I run my console application and I am logged to the VM at the same time.

But If log out it throw an error: "The handle is invalid". (As I expect the .CopyFromScreen() is no longer available)

How can I deal with this issue?

Vojtech B
  • 2,697
  • 7
  • 30
  • 57
  • 1
    If no one is logged in, you can't start an interactive process like MS Word. This will be an unsolvable problem. – Cody Gray Apr 23 '14 at 06:00
  • Ok, so is there a way how to open a session and keep it running forever? – Vojtech B Apr 23 '14 at 06:33
  • why do you have to log out? – Oliver Apr 23 '14 at 06:45
  • well, its a virtual machine and I am connecting remotely to it. As I close the session the account is logged off. – Vojtech B Apr 23 '14 at 06:50
  • As @CodyGray points out, no session = no interactive processes. You could disconnect without logging off (I'm assuming you RDP into it?), the session *should* stay open. – Alex Apr 23 '14 at 07:57
  • If the session is open it is possible to start interactive process like MS Word? – Vojtech B Apr 23 '14 at 08:48
  • I allowed multiple sessions RDP, but whenever I disconnect, the handle is no longer available. Is there really no way to do this? – Vojtech B Apr 23 '14 at 16:45
  • Ok I am cutting it into two problems. Creating virtual screen thread: http://stackoverflow.com/questions/23250862/windows-how-to-simulate-connected-screen-via-vga – Vojtech B Apr 23 '14 at 17:05
  • How is this a separate issue from your second question? What different question is being asked here? – Cody Gray Apr 23 '14 at 17:51
  • @CodyGray: Question might be the same but the formulation and possible different line of answers might be useful for more users – Vojtech B Apr 23 '14 at 20:37

0 Answers0