Using webcam in C# applications

Introduction

During our final year research project I had to find on things associated with visual images. While searching for those things I came across ways which can use web cam with a c# application. While doing digging in this area I came across various solutions, but initial ones didn’t took my interest. After going across some links I found that the most interesting thing which can use for this purpose is the AForge.NET framework. Therefore I thought of keeping a bookmark on that for anyone who is interested.

AForge.NET framework allows users to do many interesting things easily. It has already contains waste number of examples from just taking an image from a web cam to image recognition and neural networks. In most cases this AForge.NET framework will not require other libraries except basic c# libraries. In references you can find some cool things done using Aforge.NET framework.

In this post I’m going to explain on very basic application, which allows you to start & stop displaying web cam input and store a captured image. In a previous post I have done the same thing in Java using JMF framework.

Design of the application

First of all let’s have a look at the design of the application. It has two buttons, a pictureBox, and a comboBox on a Form. Their names are given as follows.

Application Design
Application Design

After the design you can start coding. But before start coding you need to download the Aforge.NET framework and add the following references to the project.

  • AForge.Video.dll
  • AForge.Video.DirectShow.dll

These files are reside inside Release folder of the downloaded folder. You can add those references through Solution Explorer window in Visual Studio. Thereafter you can add those references with ‘using’ keyword to any .cs file when required.

Coding

Now let me give a brief explanation on the coding. It initially takes the all video input devices when loading and display as a list in combo box. That is done through getCamList() method. Thereafter depending on the selected device, input will be shown in the pictureBox1. Connection between device input and pictureBox is done through a FrameEventHandler connected to video source.

Other than that basic functionality, Capture button will save the current image inside pictureBox along with the time.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video;
namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
 {
 private bool DeviceExist = false;
 private FilterInfoCollection videoDevices;
 private VideoCaptureDevice videoSource = null;
public Form1()
 {
 InitializeComponent();
 }
private void Form1_Load(object sender, EventArgs e)
 {
 getCamList();
 }
private void getCamList()
 {
 try
 {
 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
 comboBox1.Items.Clear();
 if (videoDevices.Count == 0)
 throw new ApplicationException();
DeviceExist = true;
 foreach (FilterInfo device in videoDevices)
 {
 comboBox1.Items.Add(device.Name);
 }
 comboBox1.SelectedIndex = 0;
 }
 catch (ApplicationException)
 {
 DeviceExist = false;
 MessageBox.Show("No capture device on your system");
 }
 }
private void start_Click(object sender, EventArgs e)
 {
 if (start.Text == "Start")
 {
 if (DeviceExist)
 {
 videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
 videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
 CloseVideoSource();
 videoSource.DesiredFrameSize = new Size(320, 240);
 videoSource.Start();
 start.Text = "Stop";
 }
 else
 {
  MessageBox.Show("Error: No Device selected.");
 }
 }
 else
 {
 if (videoSource.IsRunning)
 {
  CloseVideoSource();
  start.Text = "Start";
 }
 }
 }
//close the device safely
 private void CloseVideoSource()
 {
 if (!(videoSource == null))
 if (videoSource.IsRunning)
 {
 videoSource.SignalToStop();
 videoSource = null;
 }
 }
//eventhandler if new frame is ready
 private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
 {
 Bitmap img = (Bitmap)eventArgs.Frame.Clone();
 pictureBox1.Image = img;
 }
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
 CloseVideoSource();
 }
private void capture_Click(object sender, EventArgs e)
 {
  pictureBox1.Image.Save("Capture-" + DateTime.Now.ToString("HH-mm-ss tt") + ".jpg";);
 }
 }
}

The above code is the basis for any webcam application which uses Aforge.NET libraries. You are free to develop variety of other applications which using AForge.NET framework. Some of them may be object detecting systems, intelligent game applications, gesture recognizing systems, motion detecting systems, etc. Following is a snapshot of an object detecting application I created. Good luck with AForge.NET!

Simple Shape Detecting Application
Simple Shape Detecting Application

References

  1. AForge.NET framework official site – http://www.aforgenet.com/framework/
  2. AForge.NET samples – http://www.aforgenet.com/framework/samples/image_processing.html
  3. Simple webcam application – http://haryoktav.wordpress.com/2009/03/21/webcam-in-c-aforgenet/
  4. Shape Recognition application – http://leakingmemory.wordpress.com/2012/03/17/shape-recognition-using-c-and-aforge/
  5. How shape detector work – http://www.aforgenet.com/articles/shape_checker/
  6. Edge detection application – http://premsivakumar.wordpress.com/2010/12/13/edge-detection-using-c-and-aforge-net/
  7. Playing card recognition – http://www.codeproject.com/Articles/265354/Playing-Card-Recognition-Using-AForge-Net-Framewor
  8. Hand gesture recognition – http://www.codeproject.com/Articles/26280/Hands-Gesture-Recognition
  9. Recognition of handwritten digits – http://www.codeproject.com/Articles/143059/Neural-Network-for-Recognition-of-Handwritten-Digi
  10. Robotic samples – http://www.funnyrobotics.com/2010/12/brain-power-for-your-robot-aforgenet-c.html
Advertisements

7 thoughts on “Using webcam in C# applications

  1. solution is best . Thanks for this. I just want to used this in wpf application. But I get error that does not convert from system.windows.control.imagesource to bitmap. please help me to solve this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s