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:

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.

thank you Dan
but i want to use your code whose article is "C# WebCam Service with FTP and Installer" to send image from webcam to my web site whitch i create it by asp.net
please advice me what can i do exactly to apply what i want to do .
please quickly reply to me

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.

I need a little modification in the code. The functionality i am looking for are:
- To capture video(integrated laptop webcam) through time scheduling.
- Save the video on local Hard Drive.
Any idea on how to achieve this.

I was using the FtpClient class in a windows service every hour, it would run once then failed after that. I was FTPing to filezilla server, It turned out to be the default responce message from filezilla that was breaking it.

Just another little contribution if anyone stumbles upon this for a lightweight c# ftp component-

I had trouble when the socket raised an exception during file upload/download. The file handle was not being closed. Yes- there is a little more expense in throwing extra try/finallys in the upload and download methods but I think its well worth NOT having orphaned handles floating around with the O/S locking the file until windows cleans it up. So-

in the upload and download methods I've added a try/finally to ensure the file handles get closed and I also added another (nested) try/finally after the call to CreateSocket just to make sure I didnt have any orphaned socket handles either

Try doing some of the things that they posted in previous posts. Sometimes it is because your webcam is not released before it tries to take another picture. Someone posted a fix for that issue. Also, what does your error log say?

I have not gone through the code but I think problem is you are releasing the cam like...Marshal.ReleaseComObject..do not use this if you want it to run second time ... just whatever is released , make it null and service will work again , only at final closing...use Marshal.ReleaseComObject.

<code>02.12.2005 16:02:29System.NullReferenceException: Object reference not set to an instance of an object. at WebCamService.WebCamService.Run() in c:\documents and settings\a\desktop\webcamservice2\webcamservice\webcamservice.cs:line 129</code>it's in WebCam.log file...

It takes one photo and sends it to my ftp server everything is allright firstly but later every circle it gives that error and write it to file..

When i start the service it takes the first picture and uploads it to my ftp just fine but when it try's to take the second picture is when it messses up. It will delete the picture in the out folder and it wont replace it with a new one. This is what it gives me in the error log.

8/31/2005 8:00:12 PM
System.NullReferenceException: Object reference not set to an instance of an object.
at WebCamService.WebCamService.Run() in c:\projects\webcam\webcamservice\webcamservice.cs:line 129

It continues to do that untill i restart the service and then it will do the first picture and then start the same errors. anyone know what is going on?

Hi ,
I was looking for some adware,spyware free " as you say" way to get images from my webcam, ideally continuously, then I found C# WebCam Service article. But before I start, I wonder if it is possible to do it using the capture.cs, dropping some methods & repositionning some propreties? Any help would be very thankfull,
thnaks!!!

Hello everyone,
I made a simple application with the WMEncoder SDK which broadcast the images captured by the webcam. Im trying to connect to it via internet to the port which is specified in the application but I get page errors all the time.
I would appreciate an answer,
Thanks

i want to do realtime video with webcam,i have lower to both CaptureCycleSeconds &
KeepOldImageCount to 1, but refreshing rate still low. i need more frequent to do live streaming, so the capture has to be very fast.

also, since i view the jpg at ie(under ftp service), is it possible to auto refresh the browser, so that i dun have to press F5 all the time?

I've been using .NET for about 2 days now. Decided I wanted to capture images for a test project. Installed cam, (Creative WebCam 5 that was laying around) installed drivers, tried a few projects, this one worked out of the box (After I changed the image path)

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.

Everything works great and it posts the image WebCam.jpg (http://rohan.sdsu.edu/~kolte/WebCam.jpg) to the Ftp address I put in web config. But what after that? How do I exactly see the live videostream coming out of my webcam?? What address should I be using? Currently I have stopped the webcam service just because it was slowing my other applications.

I tried to go through earlier threads but I didn't find any piece of information to tell me where exactly it will show me the live streamed output.
I am confused and tried to go through the code for two hours just to understand that I am not smart enough to get it to work without help. Any help will be really appreciated. Thanks.