繁体   English   中英

在Windows Service C#中捕获图像

[英]Capturing Image in Windows Service c#

我需要创建一个Windows服务,该服务将从相机捕获图像。 搜索互联网后,我没有找到任何类似的项目。 我决定使用Aforge.net,但由于Windows Service不支持位图,因此陷入了如何捕获图像的困境。 到目前为止,这是我的代码:

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服务中不支持使用System.Drawing命名空间中的类。 尝试从这些应用程序类型之一中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常。 有关支持的替代方法,请参见Windows Imaging Components。

GDI +

Windows服务中不支持使用GDI +函​​数和类。 尝试从Windows服务中使用这些函数和类可能会产生意外问题,例如服务性能下降和运行时异常或错误。

然而!

System.Drawing确实可以在Services中运行, 只是不支持 可能存在高负载(用完非托管资源),内存或资源泄漏(实施不佳或称为处理方式)的问题

我的怀疑是您尚未引用System.Drawing.dll

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

暂无
暂无

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

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