在Windows服务中捕获图像c#

时间:2018-02-08 07:06:47

标签: c# camera windows-services aforge

我需要创建一个可以从相机捕获图像的Windows服务。在上网后,我找不到任何类似的项目。我决定使用Aforge.net但是因为在Windows服务中不支持Bitmap而陷入困境。 这是我目前的代码:

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服务中捕获图像。 请帮忙

1 个答案:

答案 0 :(得分:0)

System.Drawing Namespace

  

不支持使用System.Drawing命名空间中的类   在Windows或ASP.NET服务中。试图使用这些类   从这些应用程序类型中的一个可能产生意外   问题,例如服务性能和运行时间的减少   例外。有关支持的替代方法,请参阅Windows Imaging   组件。

GDI+

  

在Windows中不支持使用GDI +函​​数和类   服务。试图从Windows使用这些函数和类   服务可能会产生意想不到的问题,例如服务减少   性能和运行时异常或错误

<强>无论其!

System.Drawing在服务中有效,它不受支持。高负载(用完非托管资源),内存或资源泄漏(执行不当或称为部署模式)可能存在问题

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

注意 :您只需要谨慎并在试错的基础上执行此操作,但IMO保存位图应该没问题