1

我编写了以下 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[])
4

0 回答 0