There are many articles across web explaining how to access web camera. Most of them address DirectShow as a recommended approach. Since dealing with DirectShow from
.NET languages is not very trivial, I have decided to use Aforge library.
Aforge is great open source library for manipulating images and many other things including DirectShow video.
In this article I will show you how easy is to connect to WebCam and to display some user generated content on top of the video. This approach might be useful for
While I was working for biometric enrolment software for capturing ICAO compatible facial images for national documents
I wanted to use web cam as a source. After evaluating different approached, I choose one that is free and easy to use.
Using the code
I have published source code as a Visual Studio 2012 project. Language that is used in a sample is VB.NET. I believe it is easy to understand and can be easily ported to any
other .NET languages.
Import AForge Library
You can go to AForge web site and download current release of the component and include following assemblies
into the project (solution):
- AForge.dll (core)
Probably the easiest way is to use NuGet Manager (Visual Studio > Tools > Library Package Manager > Manage NuGet packages for solution) and search for online
packages, as show in the picture below.
Enumerate Video Sources and Video Modes
After you have referenced AForge libraries, you need to enumerate video sources (list of WebCams available) and their video
Private Sub EnumerateVideoDevices()
videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count <> 0 Then
For Each device As FilterInfo In videoDevices
ComboBoxSources.Items.Add("No DirectShow devices found")
ComboBoxSources.SelectedIndex = 0
Private Sub EnumerateVideoModes(device As VideoCaptureDevice)
Me.Cursor = Cursors.WaitCursor
videoCapabilities = videoDevice.VideoCapabilities
For Each capabilty As VideoCapabilities In videoCapabilities
If Not ComboBoxModes.Items.Contains(capabilty.FrameSize) Then
If videoCapabilities.Length = 0 Then
ComboBoxModes.SelectedIndex = 0
Me.Cursor = Cursors.[Default]
EnumerateVideoDevices will check for all DirectShow devices available on the computer and enumerate them in
After we get all the available devices user can choose which camera to use. This is needed in new devices, where more than one camera is present - like new Microsoft
tablet devices with front and back camera.
Once device is chosen we have to provide user a choice to select the camera resolution and speed at which the camera will deliver us new frames. For enumerating modes
for selecting camera, we use
EnumerateVideoModes function. As this process can take some time, it is recommended to change default cursor or even better
BackgroundWorker to complete this job.
Start and Stop Video
AForge.Controls assembly to add new controls to Toolbox. One of the tools added is called
VideoSourcePlayer and it is used to display
live video from camera. Below snippets shows how to start and end showing live video stream to this control.
Private Sub CameraStart()
If videoDevice IsNot Nothing Then
If (videoCapabilities IsNot Nothing) AndAlso (videoCapabilities.Length <> 0) Then
videoDevice.DesiredFrameSize = DirectCast(ComboBoxModes.SelectedItem, Size)
VideoSourcePlayer1.VideoSource = videoDevice
Private Sub CameraStop()
If VideoSourcePlayer1.VideoSource IsNot Nothing Then
VideoSourcePlayer1.VideoSource = Nothing
CameraStart assigns the selected video source and video mode to the Aforge's control
.Start method the video is shown on this component. To stop showing video you need to signal
to stop displaying video from camera source. Be aware that video thread is different than the thread of your user interface.
Add Your Own Content to the Video
It is useful to interact with the video by adding your own content to it. Sample below shows how to add custom string to the video.
By using well known
Graphics object you can interact with the video.
Private Sub VideoSourcePlayer1_NewFrame(sender As Object, _
ByRef image As Bitmap) Handles VideoSourcePlayer1.NewFrame
Dim g As Graphics = Graphics.FromImage(image)
g.DrawString("Augmented reality?", _
New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
This might be the most important part of this article, as it allows developer to access video source.
The current video frame is accessible from the parameter variable
By using the
Graphics object, it is possible to add different graphical information to the video, like displaying date and time. One could use the image to make advanced processing,
line finding an object in image or displaying some other useful information related to the current image.
I hope I have shown you how easy is to use web cameras in your projects and I hope to see some good apps using the web cameras.