Click here to Skip to main content
15,915,600 members
Articles / Programming Languages / C#
Article

C# WebCam Service with FTP and Installer

Rate me:
Please Sign up or sign in to vote.
4.68/5 (29 votes)
31 Mar 20032 min read 387.3K   3.2K   143   91
C# WebCam Windows Service with FTP upload, Windows Service Installer, and file rotation.

Introduction

I got tired looking for an app to write a snap shot from my camera to disk; that was free, no ad ware and with no advertising on the image. I found a guy who wrapped the DirectX Library in C# - WHAT A CHAMP! Here's a link to his article. Anyway, he has a full library called DShowNET and I called into that, made a Windows service that always runs when my PC is on. I added a full FTP library that is extended from the work by Jaimon Mathew.

Overview

This package comes as a project and a DLL (DShowNET). It has the following files:

  • WebCamService.cs
  • FtpClient.cs
  • Capture.cs
  • WebCamInstaller.cs

Now, using the DShowNET library is a little tricky, and just to get a photo out of the web cam tool some 400 lines of code are required. I extended the Sample Grabber supplied with DShowNET and added the reset event so that the photo could be taken synchronously.

The worst problem was making it stable, but this has been running on my workstation now for 20+ days without a restart. The most important code for stability is in the CloseInterfaces method - note the extensive try/catches - everything must be cleaned up, otherwise strange errors (including blue screens) will be observed.

The FTP library I extended, so I could use it as a full featured FTP client. I have added it to this project so I can upload files to the server.

Requirements

  • DirectX 8.1
  • .NET Framework
  • Visual Studio .NET (if you plan to edit it of course)
  • A Windows OS that supports Windows Services (2K,XP)
  • Any DirectX supported Video in device (Tuner, Capture, Camera etc...)
  • 5 Minutes of your time

Setup

The Installer component is used to install the service with the command:

C:\>installutil.exe D:\projects\webcam\webcamservice\bin\debug\webcamservice.exe

You will also need to edit the config file to reflect your needs:

XML
<add key="path" value="D:\Projects\WebCamService\out\WebCam.jpg"/>
<add key="CaptureCycleSeconds" value="10"/>
<add key="KeepOldImageCount" value="20"/>
<add key="FtpServer" value="???"/>
<add key="FtpUserName" value="???"/>
<add key="FtpPassword" value="???"/>
  • path is where you wish to upload the picture to.
  • CaptureCycleSeconds is how long to wait before taking another photo (add in capture time to 11+ seconds)
  • KeepOldImageCount is to hold the last n photos taken

The rest are pretty explanatory.

Now you cans start and stop the service with the following commands:

net start WebCamService
net stop WebCamService

The service will run in the background, and be started when your machine boots, NOT when you login.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect support.com
Australia Australia

Comments and Discussions

 
Generalconnecting to an Encoder application Pin
yaya10124-Dec-04 22:52
yaya10124-Dec-04 22:52 
GeneralRe: connecting to an Encoder application Pin
lgs.lsg29-Dec-04 19:56
lgs.lsg29-Dec-04 19:56 
GeneralRefreshing rate not fast enough Pin
Anonymous20-Dec-04 14:24
Anonymous20-Dec-04 14:24 
GeneralRe: Refreshing rate not fast enough Pin
lgs.lsg29-Dec-04 19:53
lgs.lsg29-Dec-04 19:53 
GeneralCompilation Pb Pin
odinette9-Dec-04 1:30
odinette9-Dec-04 1:30 
GeneralVidoe Cam Server Pin
Raymond Pang25-Nov-04 21:51
Raymond Pang25-Nov-04 21:51 
GeneralI'm impressed Pin
Rick Graham9-Nov-04 11:54
Rick Graham9-Nov-04 11:54 
GeneralSolution to &quot;Too many callback..&quot; error reports Pin
Bill Seddon4-Oct-04 23:10
Bill Seddon4-Oct-04 23:10 
The "real" solution to this problem probably lies deep in DirectShow.NET code. Not having the time to debug this library, my solution has worked by refactoring some of the code. The error that every one has reported occurs on the second call time the Capture class is instantiated. Specifically, the error occurs in the constructor when SetupGraph() when a SampGrabber callback is added.

In Dan's version he implemented a static function in the Capture class that creates a capture class instance, runs the code to capture an image, closes the capture class and returns an image. Because the static method is going to be used more than once in any reasonable application, the capture class is going to be created more than once and fails on the second call (at least for me). So my solution has been to implement a non-static GetImage() method in the Capture class and instantiate the Capture class at the beginning of the WebCamService's Run() method. The new, non-static method, only calls the private CaptureImage() method and returns the image. This way there is only one Capture instance and no errors.

A second advantage is that my webcam builds up a "complete" or higher quality image over a period of a couple of seconds. If the Capture class is instantiated each time a picture is needed, the image quality is consistently poor. But by instantiating the Capture class once in the Run() method of the service class, the quality is always good. It will also reduce the load on your server significantly because there’s a lot of overhead creating a Capture instance once per image.

Be aware this refactoring to work around the error does introduce another wrinkle but one that is a problem only if you want to manipulate the image returned from the new GetImage() method. My application for Dan's code is to implement a motion detector. So I want to be able to both inspect the images returned and operate upon them. With the original static function, you can be sure that the image returned will never be affected because the Capture class has been disposed. However with the refactored code the frame grabber is still operating in the background and the image returned may be altered by the frame grabber. (This appears to be because it is running in its own thread. If you put a trace statements into the new (non-static) GetImage() method before and after the call to CaptureImage() and one in the grabber’s method OnCaptureDone() there is little correlation – at least on my machine). I thought that returning a clone of the bitmap from GetImage() would be sufficient to prevent alterations to the bitmap buffer in OnCaptureDone() from affecting the image the service class receives but this does not appear to be the case. What worked for me was to use the Graphics class to draw the bitmap retrieved from DSShow.NET onto a new bitmap. Not sure why, but it prevented all access violation errors.

I hope this helps some of you

Bill Seddon
GeneralRe: Solution to &quot;Too many callback..&quot; error reports Pin
clemens8119-Jan-06 8:46
clemens8119-Jan-06 8:46 
GeneralRe: Solution to &quot;Too many callback..&quot; error reports Pin
clemens8119-Jan-06 22:04
clemens8119-Jan-06 22:04 
GeneralRe: Solution to &quot;Too many callback..&quot; error reports Pin
clemens8120-Jan-06 8:24
clemens8120-Jan-06 8:24 
GeneralRe: Solution to &quot;Too many callback..&quot; error reports Pin
Martin1138-Feb-06 18:49
Martin1138-Feb-06 18:49 
GeneralCan't get the streaming video Pin
sandeepkolte13-Jun-04 9:56
sandeepkolte13-Jun-04 9:56 
GeneralProblem Pin
COOLCAPSI8-Jun-04 10:20
COOLCAPSI8-Jun-04 10:20 
GeneralRe: Problem Pin
Rickym10-Jun-04 1:49
Rickym10-Jun-04 1:49 
GeneralFTP Problem Pin
Rickym7-Jun-04 8:01
Rickym7-Jun-04 8:01 
QuestionCan this work with photo taken using pocket pc or smartphone? Pin
fionalee24-Apr-04 6:31
fionalee24-Apr-04 6:31 
AnswerRe: Can this work with photo taken using pocket pc or smartphone? Pin
ThaBean4-Dec-04 3:43
ThaBean4-Dec-04 3:43 
GeneralChanging resolution of incoming video stream without using DsUtils.ShowCapPinDialog Pin
alexhongkong14-Mar-04 17:03
alexhongkong14-Mar-04 17:03 
GeneralRe: Changing resolution of incoming video stream without using DsUtils.ShowCapPinDialog Pin
jerfypowell15-Jan-05 18:28
jerfypowell15-Jan-05 18:28 
GeneralRe: Changing resolution of incoming video stream without using DsUtils.ShowCapPinDialog Pin
lgs.lsg30-Jan-05 18:28
lgs.lsg30-Jan-05 18:28 
QuestionCan this do any better than 1fps? Pin
Juzzam29-Oct-03 8:20
Juzzam29-Oct-03 8:20 
AnswerNevermind I figured it out Pin
Juzzam29-Oct-03 13:11
Juzzam29-Oct-03 13:11 
GeneralRe: Nevermind I figured it out Pin
Ark9-Mar-04 6:47
Ark9-Mar-04 6:47 
Generali also want the source code Pin
kkyirui20-Dec-04 16:06
kkyirui20-Dec-04 16:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.