AForge.NET

Testimonials
Features
Samples
Tools
Documentation
License
Downloads
Sources
Discussions
Partners
Projects
Members
Donate

AForge.NET Framework
2.2.5 version is available!

Access to USB cameras and video files using DirectShow

AForge.NET framework provides classes to access USB web cameras and video files using DirectShow API. Since all the classes implement common interface, accessing USB camera is made as easy, as accessing video files or JPEG/MJPEG streams.

VideoCaptureDevice class
This class allows getting access to different type of USB web cameras or other different devices, which support DirectShow interface. Below sample demonstrates accessing first available capture device in the system:

// enumerate video devices
videoDevices = new FilterInfoCollection(
        FilterCategory.VideoInputDevice );
// create video source
VideoCaptureDevice videoSource = new VideoCaptureDevice(
        videoDevices[0].MonikerString );
// set NewFrame event handler
videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame );
// start the video source
videoSource.Start( );
// ...
// signal to stop
videoSource.SignalToStop( );
// ...

private void video_NewFrame( object sender,
        NewFrameEventArgs eventArgs )
{
    // get new frame
    Bitmap bitmap = eventArgs.Frame;
    // process the frame
}

FileVideoSource class
Using this class it is possible to play video files. All we need to change in the code is to create different video source class and, since it implements the same interface, all the rest will stay as before:

// create video source
FileVideoSource videoSource = new FileVideoSource( fileName );
// set NewFrame event handler
videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame );
// start the video source
videoSource.Start( );
// ...
// signal to stop
videoSource.SignalToStop( );
// ...

private void video_NewFrame( object sender,
        NewFrameEventArgs eventArgs )
{
    // get new frame
    Bitmap bitmap = eventArgs.Frame;
    // process the frame
}