Click or drag to resize
Accord.NET (logo)

VideoCaptureDevice Class

Video source for local video capture device (for example USB webcam).
Inheritance Hierarchy
SystemObject
  Accord.Video.DirectShowVideoCaptureDevice

Namespace:  Accord.Video.DirectShow
Assembly:  Accord.Video.DirectShow (in Accord.Video.DirectShow.dll) Version: 3.8.0
Syntax
public class VideoCaptureDevice : IVideoSource
Request Example View Source

The VideoCaptureDevice type exposes the following members.

Constructors
  NameDescription
Public methodVideoCaptureDevice
Initializes a new instance of the VideoCaptureDevice class.
Public methodVideoCaptureDevice(String)
Initializes a new instance of the VideoCaptureDevice class.
Public methodVideoCaptureDevice(String, PixelFormat)
Initializes a new instance of the VideoCaptureDevice class.
Top
Properties
  NameDescription
Public propertyAvailableCrossbarVideoInputs
Available inputs of the video capture card.
Public propertyBytesReceived
Received bytes count.
Public propertyCrossbarVideoInput
Current video input of capture card.
Public propertyDesiredAverageTimePerFrame
The desired average display time of the video frames, in 100-nanosecond units. There is no guarantee that the device will actually respect this setting, however some devices will not work unless this property is set. See remarks for details.
Public propertyDesiredFrameRate Obsolete.
Obsolete - no longer in use.
Public propertyDesiredFrameSize Obsolete.
Obsolete - no longer in use
Public propertyDesiredSnapshotSize Obsolete.
Obsolete - no longer in use
Public propertyFramesReceived
Received frames count.
Public propertyIsRunning
State of the video source.
Public propertyProvideSnapshots
Specifies if snapshots should be provided or not.
Public propertySnapshotCapabilities
Snapshot capabilities of the device.
Public propertySnapshotResolution
Snapshot resolution to set.
Public propertySource
Video source.
Public propertySourceObject
Source COM object of camera capture device.
Public propertyVideoCapabilities
Video capabilities of the device.
Public propertyVideoResolution
Video resolution to set.
Top
Methods
  NameDescription
Public methodCheckIfCrossbarAvailable
Check if running video source provides crossbar for configuration.
Public methodDisplayCrossbarPropertyPage
Display property page of video crossbar (Analog Video Crossbar filter).
Public methodDisplayPropertyPage
Display property window for the video capture device providing its configuration capabilities.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetCameraProperty
Gets the current setting of a camera property.
Public methodGetCameraPropertyRange
Gets the range and default value of a specified camera property.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodSetCameraProperty
Sets a specified property on the camera.
Public methodSignalToStop
Signal video source to stop its work.
Public methodSimulateTrigger
Simulates an external trigger.
Public methodStart
Start video source.
Public methodStop
Stop video source.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodWaitForStop
Wait for video source has stopped.
Top
Events
  NameDescription
Public eventNewFrame
New frame event.
Public eventPlayingFinished
Video playing finished event.
Public eventSnapshotFrame
Snapshot frame event.
Public eventVideoSourceError
Video source error event.
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

This video source class captures video data from local video capture device, like USB web camera (or internal), frame grabber, capture board - anything which supports DirectShow interface. For devices which has a shutter button or support external software triggering, the class also allows to do snapshots. Both video size and snapshot size can be configured.

Examples
// To use the VideoCaptureClass, the first step is to enumerate the different
// video capture devices in your system. The output of the method below should
// include any webcam currently attached to or integrated into your computer:
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

// A possible output will be: 
// "{ Integrated Webcam(@device:pnp:\\?\usb#vid_0c45&pid_6713&mi_00#6&4851259&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global)}"

// Now, we can create a video source selecting one of the devices. For this,
// pass the DirectShow device moniker (a kind of device identifier) of the 
// device you would like to capture from to VideoCaptureDevice:
var videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);

// Then, we just have to define what we would like to do once the device send 
// us a new frame. This can be done using standard .NET events (the actual 
// contents of the video_NewFrame method is shown at the bottom of this page)
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);

// Start the video source. This is not a blocking operation, meaning that 
// the frame capturing routine will actually be running in a different thread 
// and execution will be returned to the caller while the capture happens in 
// the background:
videoSource.Start();

// ...

// Let's say our application continues running, and after a while we decide
// to stop capturing frames from the device. To stop the device, we can use

videoSource.SignalToStop();

// Note that this is again a non-blocking operation. The device will continue
// capturing for a few milliseconds until it gets notified about our request.

The video_NewFrame event could have been defined as in any of the following examples:

// The video_NewFrame used in the example above could be defined as:
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    // get new frame
    Bitmap bitmap = eventArgs.Frame;

    // process the frame (you can do anything with it, such as running an
    // image processing filter, saving it to disk, showing on screen, etc)
}
// This is anowher example on how the video_NewFrame could have been 
// defined. In this example, we will apply a grayscale filter to the
// images as they arrive from the capture device.

// When processing multiple frames, it is better to try to cache
// all operations and memory allocations outside of the main loop:
Grayscale grayscale = Grayscale.CommonAlgorithms.BT709;

// The video_NewFrame used in the example above could be defined as:
private void video_NewFrame2(object sender, NewFrameEventArgs eventArgs)
{
    // get new frame
    Bitmap bitmap = eventArgs.Frame;

    // Apply the filter we have cached outside:
    Bitmap grayBitmap = grayscale.Apply(bitmap);

    // process the frame (you can do anything with it, such as running an
    // image processing filter, saving it to disk, showing on screen, etc)
}
See Also