C#监视Windows服务的桌面屏幕

By simon at 2018-02-28 • 0人收藏 • 91人看过

我刚刚编写了C#代码,它记录了该屏幕截图 它在WinForms上工作,但不适用于Windows Se服务。 我的代码如下:

public partial class ScreenCapture : ServiceBase
{
        bool rec = false;

        Rectangle screenSize = Screen.PrimaryScreen.Bounds;

        UInt32 frameCount = 0;

        VideoFileWriter writer;

        int width = SystemInformation.VirtualScreen.Width;
        int height = SystemInformation.VirtualScreen.Height;

        AForge.Video.ScreenCaptureStream streamVideo;

        Stopwatch stopWatch = new Stopwatch();

        public ScreenCapture()
        {
            InitializeComponent();
            writer = new VideoFileWriter();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyLog");
            }
            eventLog1.Source = "MySource";
            this.CanHandleSessionChangeEvent = true;
        }

        protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            string folderName = @"C:\LoginLog";
            if (changeDescription.Reason == SessionChangeReason.SessionLogon)
            {
                eventLog1.WriteEntry(changeDescription.SessionId + " User Logon");
                if (!Directory.Exists(folderName))
                {
                    Directory.CreateDirectory(folderName);
                }

                StartRec(folderName);
            }
            else if (changeDescription.Reason == SessionChangeReason.SessionLogoff)
            {
                eventLog1.WriteEntry(changeDescription.SessionId + " User Logoff");
                rec = false;
            }
            else if (changeDescription.Reason == SessionChangeReason.SessionLock)
            {
                eventLog1.WriteEntry(changeDescription.SessionId + " User Lock");
                rec = false;
            }
            else if (changeDescription.Reason == SessionChangeReason.SessionUnlock)
            {
                eventLog1.WriteEntry(changeDescription.SessionId + " User Unlock");
                if (!Directory.Exists(folderName))
                {
                    Directory.CreateDirectory(folderName);
                }

                StartRec(folderName);
            }
            base.OnSessionChange(changeDescription);
        }

        private void StartRec(string path)
        {
            if (rec == false)
            {
                rec = true;

                frameCount = 0;

                string time = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ssff");
                string compName = Environment.UserName;
                string fullName = path + "\\" + compName.ToUpper() + "_" + time;

                try
                {
                    writer.Open(
                        fullName + ".mp4",
                        width,
                        height,
                        10,
                        VideoCodec.MPEG4, 1000000);
                }
                catch (Exception ex)
                {
                    eventLog1.WriteEntry("Exception StartRec: " + ex.Message);
                }

                DoJob();
            }
        }

        private void DoJob()
        {
            try
            {
                Rectangle screenArea = Rectangle.Empty;
                foreach (System.Windows.Forms.Screen screen in
                    System.Windows.Forms.Screen.AllScreens)
                {
                    screenArea = Rectangle.Union(screenArea, screen.Bounds);
                }

                streamVideo = new ScreenCaptureStream(screenArea);

                streamVideo.NewFrame += new NewFrameEventHandler(video_NewFrame);

                streamVideo.Start();

                stopWatch.Start();
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Exception DoJob: " + ex.Message);
            }
        }

        private void video_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            try
            {
                if (rec)
                {
                    frameCount++;
                    writer.WriteVideoFrame(eventArgs.Frame);
                }
                else
                {
                    stopWatch.Reset();
                    Thread.Sleep(500);
                    streamVideo.SignalToStop();
                    Thread.Sleep(500);
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Exception Video New Frame: " + ex.Message);
            }
        }
}
该服务可以保存一个.mp4文件,但它无法打开它。我想windows 服务不能捕获桌面屏幕。 任何人都可以帮助我解决这个问题吗? 提前致谢。

1 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

您需要在交互式帐户下设置Windows服务运行, 要做到这一点,你需要去服务的属性登录选项卡并通过登录 你的Windows帐户。 [![enter image description 这里](https://i.stack.imgur.com/gP4be.png)](HTTPS://i.stack.imgur的.com / gP4be.png)

登录后方可回帖

Loading...