Click here to Skip to main content
15,905,028 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 386.6K   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

 
GeneralNeed help getting it to work Pin
Juzzam28-Oct-03 19:04
Juzzam28-Oct-03 19:04 
GeneralRe: Need help getting it to work Pin
Juzzam28-Oct-03 19:33
Juzzam28-Oct-03 19:33 
QuestionWatched Threads? Pin
m2N24-Oct-03 2:47
m2N24-Oct-03 2:47 
AnswerRe: Watched Threads? Pin
m2N24-Oct-03 4:41
m2N24-Oct-03 4:41 
GeneralFtpClient Pin
El Dorko28-Apr-03 5:28
El Dorko28-Apr-03 5:28 
GeneralRe: FtpClient Pin
Someone54383-Jun-03 22:31
Someone54383-Jun-03 22:31 
GeneralRe: FtpClient Pin
Ian Turner (syzygy)4-Jun-03 1:03
sussIan Turner (syzygy)4-Jun-03 1:03 
GeneralRe: FtpClient Pin
Craig Arnold22-Jul-03 1:30
Craig Arnold22-Jul-03 1:30 
A bug exists where you get back multi-line replies from an ftp server.

So I've hacked a fix for it. Not thoroughly tested, but my app is working. Smile | :)

Replace the original nextLine() method with a new nextLine() method and a GetNextLine() method:

//***start of hack!

private string GetNextLine()
{
string nextline = "";
while(true)
{
this.bytes = clientSocket.Receive( this.buffer, this.buffer.Length, 0 );
nextline += ASCII.GetString( this.buffer, 0, this.bytes );

if ( this.bytes < this.buffer.Length )
{
break;
}
}
return nextline;
}

private string readLine()
{
this.message = GetNextLine();

//check for multi-line message
if (this.message.Length > 4 && this.message.Substring(3,1).Equals("-"))
{
//get the reply code
string startcode = this.message.Substring(0,3);
string linecode = "";
string nextline = "";

while(true)
{
nextline = GetNextLine();
if ( this.verboseDebugging ) Debug.WriteLine("Next line of message is : " + nextline);

if (nextline.Length > 3)
{
linecode = nextline.Substring(0,3);
if ((linecode == startcode) && nextline.Substring(3,1).Equals(" ")) //last line
{
//simple replace will do as the last line contains the message code.
this.message = nextline;
break;
}
}
}
}
return message;
}

//****End of hack!

Here is an extract from RFC959.

An FTP reply consists of a three digit number (transmitted as three alphanumeric characters) followed by some text. The number is intended for use by automata to determine what state to enter next; the text is intended for the human user. It is intended that the three digits contain enough encoded information that the user-process (the User-PI) will not need to examine the text and may either discard it or pass it on to the user, as appropriate. In particular, the text may be server-dependent, so there are likely
to be varying texts for each reply code.

A reply is defined to contain the 3-digit code, followed by Space <sp>, followed by one line of text (where some maximum line length has been specified), and terminated by the Telnet end-of-line code. There will be cases however, where the text is longer than a single line. In these cases the complete text must be bracketed so the User-process knows when it may stop reading the reply (i.e. stop processing input on the control connection) and go do other things. This requires a special format on the first line to indicate that more than one line is coming, and another on the last line to designate it as the last. At least one of these must contain the appropriate reply code to indicate the state of the transaction. To satisfy all factions, it was decided that both the first and last line codes should be the same.

Thus the format for multi-line replies is that the first line will begin with the exact required reply code, followed immediately by a Hyphen, "-" (also known as Minus), followed by text. The last line will begin with the same code, followed immediately by Space <sp>, optionally some text, and the Telnet end-of-line code.

For example:


123-First line
Second line
234 A line beginning with numbers
123 The last line

The user-process then simply needs to search for the second occurrence of the same reply code, followed by <sp> (Space), at the beginning of a line, and ignore all intermediary lines. If an intermediary line begins with a 3-digit number, the Server must pad the front to avoid confusion.
GeneralRe: FtpClient Pin
Craig Arnold22-Jul-03 3:03
Craig Arnold22-Jul-03 3:03 
GeneralMultiple calls to Capture Pin
robotech11-Apr-03 16:32
robotech11-Apr-03 16:32 
GeneralRe: Multiple calls to Capture Pin
Dan Glass14-Apr-03 5:51
Dan Glass14-Apr-03 5:51 
GeneralRe: Multiple calls to Capture Pin
Philip Fitzsimons20-Jul-03 8:38
Philip Fitzsimons20-Jul-03 8:38 
GeneralRe: Multiple calls to Capture Pin
Piers Lawson19-Oct-03 22:21
Piers Lawson19-Oct-03 22:21 
GeneralRe: Multiple calls to Capture Pin
Philip Fitzsimons19-Oct-03 23:26
Philip Fitzsimons19-Oct-03 23:26 
GeneralRe: Multiple calls to Capture Pin
Piers Lawson20-Oct-03 1:47
Piers Lawson20-Oct-03 1:47 
GeneralRe: Multiple calls to Capture Pin
Philip Fitzsimons21-Oct-03 2:27
Philip Fitzsimons21-Oct-03 2:27 
GeneralRe: Multiple calls to Capture Pin
Piers Lawson21-Oct-03 2:36
Piers Lawson21-Oct-03 2:36 
GeneralRe: Multiple calls to Capture Pin
Piers Lawson26-Oct-03 1:06
Piers Lawson26-Oct-03 1:06 
QuestionRe: Multiple calls to Capture Pin
someone999-Oct-07 6:09
someone999-Oct-07 6:09 
GeneralRe: Multiple calls to Capture Pin
lunarseacow20-Nov-08 20:59
lunarseacow20-Nov-08 20:59 
GeneralRe: Multiple calls to Capture Pin
herceg.tomas3-Jan-09 8:07
herceg.tomas3-Jan-09 8:07 
QuestionWrapper ? Pin
nidhogg1-Apr-03 20:56
nidhogg1-Apr-03 20:56 
AnswerRe: Wrapper ? Pin
Dan Glass2-Apr-03 9:04
Dan Glass2-Apr-03 9:04 
GeneralDirectX 9 Pin
Jason Douglas28-Mar-03 3:49
professionalJason Douglas28-Mar-03 3:49 
GeneralRe: DirectX 9 Pin
Dan Glass28-Mar-03 6:05
Dan Glass28-Mar-03 6:05 

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.