A Series of Webcam Tests in C#
Test 1: Using WIA to Capture "Snapshots"

For all of these tests I used a Logitech c525 USB webcam. It is important to note that this webcam does not support taking single pictures or snapshots, and my goal was to try and simulate this feature as closely as possible.

This first test was actually a very simple project. I started with the source code I found here, which actually complied very nicely in Visual C# express 2010. I tidied up the interface a fair bit, and added a textbox that checks for an integer value when you press "Start". The textbox sets an interval in ms for how frequently a snapshot should be taken. I thought that doing this in WIA makes the most sense and was the easiest to implement. The biggest problem with this program (and this problem is carried over to the next 2 test programs as well) is that I wasn't able to figure out how to resize the captured video. Aside from that, the snapshot function worked very well!

Test 2: Adding the Ability to Record Video Using DirectShow.Net

Source/project files download (Visual C# Express 2010).

The next thing I wanted to do, mostly for fun, was record video using a webcam. The first thing I tried was DirectShow.Net. I downloaded the library files, but for whatever reason I still had problems recording video. I had to also add a reference for DirectShowLib-2005.dll, which is located with the source code files. The bulk of the code I used for the video capture aspect of this program came from the sample project located in Samples -> Capture -> CapWMV, which can be found through that link above.

Unfortunately I was very unhappy with the quality of video that was recorded, so I decided to look for another method.

Test 3: Snapshots in WIA; Video Recordings Using Aforge.Net

Here's the source and project files (Visual C# Express 2010)

I ran into a lot of problems with this program, but thankfully the end result is a much higher quality video recording.

  • Download the Aforge.net libraries and install Aforge.net here.
  • Have to include references to a bunch of downloaded Aforge.net libraries.
  • Had to turn on "optimize code" in Build section of Project Properties to help with memory problems caused by recording several videos in a row without restarting the program.
  • Make sure all the .dlls installed by Aforge.net (located in C:\Program Files (x86)\AForge.NET\Framework\Externals\ffmpeg\bin) are also in the same directory as your project's .exe. You need to do this manually! Adding the references to the other .dlls is not sufficient for FFMPEG.
  • Add an application configuration file to project containing the following within the configuration tags.
      
    <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
    <supportedRuntime version="v2.0.50727"></supportedRuntime>
    </startup>
    
  • The actual code I used to get the video to record was found in an answer on stackoverflow. The code that I adapted from is here (in the answer that just says "In this way ;)").

Test 4: Using AForge.NET for both Snapshot and Video Recording

Source code and project files (Visual C# Express 2010)

This was the biggest challenge so far. I was able to get a basic version of the snapshot working using Aforge.NET, but in a pretty ugly way. This program has 2 pictureboxes, one for the 'fluid' video, and one for the snapshot view. Both pictureboxes are always being manipulated by the program, but only one is visible at a time depending on what you're doing. When snapshot mode is engaged by clicking a button, 2 timers start running simultaneously, each taking a turn at flicking a boolean variable called snapshotTrigger on or off. When the 'fluid' video needs a new frame, the program checks to see what the variable is set to. When the program calls the FinalVideo_NewFrame function, there is a line that checks to see what this variable is set to. If it is true, then pictureBox2 gets an updated image. This variable is only true one moment for every interval, however, so most of the time the function finds that the value is false and does not update pictureBox2.

This program also still records in .avi, but it does not record the "snap shots", only the 'fluid' video.

An interesting problem I have yet to figure out occurs in Form1_FormClosing ... if the MessageBox does not pop up in between FileWriter.Close(); and this.FinalVideo.Stop();, the program exits with a mess behind it - a process remaining in the background. I tried Thread.Timer(), but this did not fix the problem. Having a MessageBox pop up instead does fix the problem, but I wish I understood why.

Obviously, all the steps I listed from Test 3 carried onto this version of the program. This code is the cleanest and most functional thus far.

Finale: Webcam Recorder Using AForge.NET

Project files and whatnot (Visual C# Express 2010)

Just so I can end this series of tests with a usable program, I took the program from Test 4 above, removed all the snapshot code (because, really, who needs that?) and converted the program into a pure Webcam recorder. You connect to your Webcam, it automatically resizes the window based on the resolution you chose, and then you can record video! That's it! At least it's sort of useful, right??