C# Windows Camera Application

This is a simple application that detects a camera connected the computer, shows a live image of the camera, previews snapshots and saves the resulting image. The app takes user information from a form and uses them for the name of the image file to be saved.

It is written for .NET platforms. I used C# and Visual Studio 2010.

cameraApp

This is a description of the code behind the application and a few useful C# tricks.

AForge is a powerful framework for computer vision, image processing and many other image-related projects. For this project in particular, I was interested in its capabilities to detect any cameras connected to a computer.

First, we add the required references to the project Project > Add References. They are also added to the code:

The following code detects any video capture devices connected to the computer and adds them to a combo box. The selected device is started after button1 is clicked.

Taking a picture

Then, btnSnapshot takes a preview of the live image and shows it at a second picture box.

Preventing app from taking over

If the user closes the application before stopping the camera, the application continues running in the background and using up memory. To avoid this, we check if the camera has not been stopped before attempting to close the app and then close it if needed.

Selecting path to save

Select the Desktop folder as the predetermined location for saving generated images. However, Windows generally keeps the Desktop folder at C:\Users\username\Desktop. By using the following code, we find the path of the Desktop folder for future use.

The user can change the save location by choosing a new folder with button2

The newly selected path becomes the save location:

Saving an Image

First, we check is there is an image already on the Preview box for saving. If so, we create the file name by concatenating the user input from the information form into a string strFilename . Finally, we create a namePath string containing both strFilename and saveLocation from before.

Unless the application is run with elevated administrator privileges, it will not save to C:/ directly.

Automatically move cursor

When the application loads, the cursor is blinking on the first text box, ready for input. Doing this requires setting the Focus property on the desired textbox.

Also, the cursor automatically moves from one textbox to the next on the Date of Birth field. On txtMonth and txtDay the App checks expects two characters. When the condition is met, the Focus property is set to the next text box.

Distributing the application

Because this application uses .dll files from the AForge package, these dlls need to be available in the same directory where the application is running. There are two options to achieve this:

1. Create a setup project for distribution on Visual Studio 2010. Or,

2. Include the dll files in the build. The three files below are required. Notice that Aforge.dll was not referenced directly at the beginning of the program. However, Aforge.Video and AForge.Video.DirecShow need it.

Two alternatives to merge the dlls:

Command line ILMedge
GUI ILMedge

Using ILMerge allows the creation of an .exe file capable of running on a .NET platform without adding the AForge .dll files. The application becomes fully portable.