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,837
  • 7
  • 31
  • 59

0 Answers0