在 Windows 服务 c# 中捕获图像

Capturing Image in Windows Service c#

我需要创建一个 Windows 服务来从相机捕捉图像。搜索互联网后,我没有找到任何类似的项目。我决定使用 Aforge.net,但由于 windows 服务不支持位图,所以卡住了如何捕获图像。 到目前为止,这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Deployment;
using System.Runtime.InteropServices;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;



namespace PCSecurityCamera
{
    partial class PCSecurityCamera : ServiceBase

    {
        System.Timers.Timer timeDelay;

        string pixDrive = "", journalLoc = "", txnDate = "", txnTime = "", txnDate1 = "";
        int retVal, timeFrame = 0, count = 0, txn_count = 0, retention = 0;
        string picdirectory;
        int i = 0;

        string[] availableCameras = new string[5];
        private FilterInfoCollection VideoCaptureDevices; //stores all available camera
        private VideoCaptureDevice FinalVideoSource; //stores camera to be used

        public PCSecurityCamera()
        {
            InitializeComponent();
            timeDelay = new System.Timers.Timer();
            timeDelay.Elapsed += new System.Timers.ElapsedEventHandler(WorkProcess);
        }

        public void WorkProcess(object sender, System.Timers.ElapsedEventArgs e)
        {

        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            LogService("PCSecuritycamera Service is Started");
            try
            {
                int camCount = 0;
                Array.Clear(availableCameras,0,availableCameras.Length);
                VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                foreach(FilterInfo VideoCaptureDevice in VideoCaptureDevices)
                {
                    availableCameras[camCount] = VideoCaptureDevice.Name.ToString();
                    LogService(availableCameras[camCount]);
                    camCount++;
                }
                if (availableCameras[0] == "")
                {
                    LogService("No Available Camera");
                }
                else
                {
                    FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
                    LogService("Camera Selected: " + FinalVideoSource.ToString());
                    FinalVideoSource.NewFrame +=FinalVideoSource_NewFrame;
                }


            }
            catch (Exception e)
            {
                LogService(e.ToString());
            }


            timeDelay.Enabled = true;


        }

        private void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {

        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            LogService("Service Stoped");
            timeDelay.Enabled = false;
        }
        private void LogService(string content)
        {
            FileStream fs = new FileStream(@"C:\Users\talatj\Desktop\Me\ServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(content);
            sw.Flush();
            sw.Close();
        }
    }
}

我的问题是如何在 windows 服务中捕获图像。 请帮助

System.Drawing Namespace

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.

GDI+

GDI+ functions and classes are not supported for use within a Windows service. Attempting to use these functions and classes from a Windows service may produce unexpected problems, such as diminished service performance and run-time exceptions or errors

然而!

System.Drawing 在服务中确实有效,只是不受支持。可能存在高负载(运行 非托管资源)、内存或资源泄漏(实施不当或称为处置模式)

的问题

我怀疑你没有引用 System.Drawing.dll

注意 :你只需要保持警惕并在反复试验的基础上执行此操作,尽管 IMO 保存位图应该没问题