Josua Talatala
Josua Talatala

Reputation: 39

Capturing Image in Windows Service c#

I need to create an Windows Service that will capture images from camera. After serching the internet, i do not find any similar project. I decided to use Aforge.net but got stuck in how to capture image because the Bitmap is not supported in windows Service. here is my code so far:

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();
        }
    }
}

my problem is how to capture the image in windows service. Please help

Upvotes: 1

Views: 892

Answers (1)

TheGeneral
TheGeneral

Reputation: 81503

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

HOWEVER!

System.Drawing does work in Services, it's just not supported. There can be issues with high load (running out of unmanaged resources), memory or resource leaks (badly implemented or called dispose patterns)

My suspicions is you have just not referenced the System.Drawing.dll

Note : You will just have to be wary and do this on a trial and error basis, though IMO saving bitmaps should be fine

Upvotes: 1

Related Questions