次のc#コードを記述し、サーバー上でスケジュールされたタスクとして実行したいと思います。ただし、コードのこの部分が原因で例外が発生します。エラーは画面がないという事実に関係していると思います。誰かが画面をスケジュールされたタスクとしてキャプチャし、リモートシステムに接続していない方法を見つけましたか?
Bitmap bt;
// a browser is running for which screen shot needs to be taken.
bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format24bppRgb);//use 24bbp for capture image as AForge supports only 24bpp.
Graphics screenShot = Graphics.FromImage(bt);
screenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bt.Save(@"C:\folder\test.png", ImageFormat.Png);
サーバー上で実行しているため、物理モニターが接続されていないため、サーバーにリモート接続する必要があります。手動で実行してもエラーはありません。ただし、スケジュールを設定してマシンからログオフすると、次のメッセージが表示されます
Application: ConsoleAppCaptureScreenshot.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ComponentModel.Win32Exception
Stack:
at System.Drawing.Graphics.CopyFromScreen(Int32, Int32, Int32, Int32, System.Drawing.Size, System.Drawing.CopyPixelOperation)
at ConsoleAppCaptureScreenshot.Program.Main(System.String[])