Search This Blog

Sunday, July 4, 2010

Webcam using AForge.NET (Part I)


Last tutorial we saw about setting up the environment to use AForge.NET. That was not a necessary tutorial, but it was my first post to see how to use windows live writer tool. And also that will give a sense satisfaction for people who have used OpenCV and trying AForge for the first time.

So now we have a window look like this. ( you can refer the previous tutorial to see how we set this environment).



Now the fun part. Programming !!!!!!!!!!!!!!!!!!!!!!!!!!

I am an Electrical guy, therefore according to records I am not a good programmer.

Press the “F7” function key to go the see the code of this form.

[or] right click on the form and select “View Code”


             It is the same.

We will see a page like this


This is called the “code behind”. The previous one is called the “design view”. These are non standard terminologies used in programming environment. Just keep yourself updated because we’ll be using this in the upcoming tutorials.

Add these two namespaces


Before we use the webcam the code has to see the webcam (or ) the program has to know that there is a web cam attached to the computer. So here is our first aim

Aim : To list the USB camera attached to my computer


Put a button in the form. So when we click on the button, it is going to popup box for each camera.


Double click on the button and it will create a function. They call it the event handler. So when the applications is running and we click on the button, it will go to this function.


Now we are going to add code into this function so when we click the button it should list the USB camera attached to this computer (or laptop in my case). I will add the code and then explain the code with inline comments. I don’t want to add more screenshots and make this boring. So look at the following screenshot and try to read and understand the inline comments which will explain what is what.


Now lets compile the code using the shortcut key (im sorry, im used to it. Sort of fun) [ Ctrl + Shift + b ] or you can directly run it by pressing the function key “F5”. But “F5” will properly execute only if the code has no errors.

Lets see what result I got when I ran this program. I got 2 Camera’s in my laptop. So lets see if it lists them all. The following screenshots are my outputs.

I got 2 outputs !!!

1st Camera - HP Webcam



2nd Camera – USB camera



Awesome!!!!!!!!!!!!!!!!!!! AForge.NET got the list of camera’s connected to my computer.

 Next tutorial is how to read video from a USB camera (a webcam in my case).




  1. Thanks this is really halpful...but it gives me a error like this
    The name 'usbCams' does not exist in the current context

  2. Add this line in the class variable (check the image, you might have forgot to add this).

    private FilterInfoCollection usbCams;


  3. Thanks a lot for the very informative article. Keep it up...!!!

  4. THANKS A lot prem,,this is awsome article ,,,but if we want to capture the image in frame per second ,,how it can done ...... help ma :-)

  5. @abhi: can you be clear? did you go through the 2nd part of the post? if that does not help you let me know.


  6. thanks prem, this is so helpful for a rookie like me :)

  7. Thanks for the post Prem.It was really helpful.Can u please tell me that how u got to know that which classes u have to use in it like as "FilterInfoCollection" and "FilterCategory".