【问题标题】:Capturing Image in Windows Service c#在 Windows 服务 c# 中捕获图像
【发布时间】:2018-02-08 07:06:46
【问题描述】:

我需要创建一个从相机捕获图像的 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 服务中捕获图像。 请帮忙

【问题讨论】:

  • 什么错误

标签: c# camera windows-services aforge


【解决方案1】:

System.Drawing Namespace

不支持使用 System.Drawing 命名空间中的类 在 Windows 或 ASP.NET 服务中。尝试使用这些类 从这些应用程序类型之一可能会产生意想不到的 问题,例如降低服务性能和运行时间 例外。有关受支持的替代方案,请参阅 Windows 映像 组件。

GDI+

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

但是!

System.Drawing 可以在服务中使用,只是不支持。可能存在高负载(耗尽非托管资源)、内存或资源泄漏(实施不当或称为处置模式)的问题

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

注意:您只需要小心并在反复试验的基础上这样做,尽管 IMO 保存位图应该没问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2012-11-09
    相关资源
    最近更新 更多