Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WindowsServiceC中捕获图像#_C#_Camera_Windows Services_Aforge - Fatal编程技术网

C# 在WindowsServiceC中捕获图像#

C# 在WindowsServiceC中捕获图像#,c#,camera,windows-services,aforge,C#,Camera,Windows Services,Aforge,我需要创建一个Windows服务,将从相机捕获图像。上网后,我没有发现任何类似的项目。我决定使用一个org.net,但由于windows服务不支持位图,我在如何捕获图像方面遇到了困难。 以下是我目前的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Diagnostics; us

我需要创建一个Windows服务,将从相机捕获图像。上网后,我没有发现任何类似的项目。我决定使用一个org.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命名空间中的类 在Windows或ASP.NET服务中。正在尝试使用这些类 从这些应用程序类型中的一个可能会产生意外的 问题,例如服务性能降低和运行时间缩短 例外情况。有关受支持的替代方案,请参阅Windows映像 组成部分

Windows系统中不支持使用GDI+函数和类 服务试图从Windows应用程序中使用这些函数和类 服务可能会产生意外问题,例如服务减少 性能和运行时异常或错误

然而

System.Drawing
在服务中可以工作,只是不受支持。可能存在高负载(非托管资源耗尽)、内存或资源泄漏(实现不当或称为dispose模式)等问题

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


注意:您只需小心,并在试错的基础上进行操作,尽管我保存位图应该可以

错误是什么