简体   繁体   English

C# 从 Windows 服务监控桌面屏幕

[英]C# Monitoring desktop screen from a Windows service

I have just written the C# Code that is recording the screenshot of the desktop.It works on Winforms but not works in windows service.我刚刚编写了记录桌面屏幕截图的 C# 代码。它适用于 Winforms,但不适用于 Windows 服务。

My code as below:我的代码如下:

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);
            }
        }
}

And the service can save a .mp4 file, but it cannot open it.I think windows service can not capture desktop screen.并且该服务可以保存一个.mp4文件,但无法打开它。我认为windows服务无法捕获桌面屏幕。

Can anybody help me how to solve this problem?有人可以帮我解决这个问题吗?

thanks in advance.提前致谢。

You need to set windows service run under interactive account,您需要设置在交互式帐户下运行的windows服务,

To do so you need to go to properties of service got to logon tab and login by your windows account.为此,您需要转到服务属性,转到登录选项卡并使用您的 Windows 帐户登录。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM