Here's a library you can use from
.NET to access Video Capture Devices, such as webcams, that are attached
to your computer. You can grab frames off of them and use those
frames in your own application.
VideoCaptureNET comes as single DLL that you
can add as a reference to a C#, VB, or Managed C++ project. It is written in a hybrid of
Managed and Unmanaged C++, and leverages DirectShow. However, when
you're working with it, you don't need to know that -- it looks and feels
similar to the other parts of the .NET
Framework.
VideoCaptureNET is released under the
GPL.
You are free to use it for non-commercial applications, so long as you
provide credit for using the library -- and
write to
me to tell me how you're using it!
Basic Functionality
Add the VideoCaptureNET
DLL to your C#, VB, or Managed C++ project, and you will find the
Types you are looking for by using the Allenwood.VideoCaptureNet
namespace.
// Get the
default
device.
VideoCaptureDevice vcd = VideoCaptureDevice.GetDefaultDevice();
// Handle the FrameCaptured event to get new frames.
vcd.FrameCaptured += new
VideoCaptureFrameEventHandler(OnFrameCaptured);
// Enable the device to start receiving frames.
vcd.Enabled = true;
Your method to handle
captured video frames can look like this:
private void
OnFrameCaptured(object sender, VideoCaptureFrameEventArgs e)
{
// You can obtain
the raw RGB bytes from the frame.
byte[] rawFrameRgbBytes = e.Bytes;
// Or, you can obtain the frame as a System.Drawing.Bitmap.
Bitmap frameAsABitmap = e.GetBitmap();
}
Additional Functionality
// You can
change
brightness, contrast and other settings like this:
vcd.Properties.Brightness.Value = 60;
vcd.Properties.Brightness.Value =
vcd.Properties.Brightness.DefaultValue;
// You can work with a non-default video
capture device using something like this:
VideoCaptureDeviceDesc[] devices =
VideoCaptureDeviceDesc.GetAvailableDeviceDescs();
VideoCaptureDevice vcd1 = new VideoCaptureDevice(devices[1]);
// You can change the input resolution to one of the valid options
for the device like this:
Size[] availableResolutions = vcd.GetResolutionCaps();
vcd.Resolution = availableResolutions[0];
Demo included in the
download
I have included in the
download a Visual Studio 2003 project, TestVideoCaptureNet, that enumerates the video
capture devices attached to your computer, lets you view as many
streams as you'd like, and lets you adjust the brightness, contrast,
and resolution of any video stream you are watching.
This is a beta release
I have not thoroughly tested this to ensure
there are no memory leaks on the C++ side!! I've developed it so
that the user of the library has no access to unmanaged memory.
However, I am not 100% sure that there are no leaks involved in the
creation and destruction of the DirectShow graph. If you are good at
this sort of thing, I would be very much obliged if you could check it
over and let me know if there is any hidden nastiness (aside from the
hideous code itself).
Alas, I'm not going to have time to clean this
up any time soon, but I've had a lot of requests for this
library, so I thought I'd make it available. Hopefully you'll find
it useful, but it comes with no guarantees, and -- especially before I
do a thorough memory-leak test -- please understand that this shouldn't
be used in
production-quality code.
Once again, I (Rob) hold the copyright on
VideoCaptureNet, as I developed it while not employed by any
organization. I'm releasing it under the GPL, so you are free to
use it for non-commercial applications, and I would be very much obliged
to hear if you find it useful for a project you're working on!
I welcome
comments, requests,
suggestions and contributions.
Download VideoCaptureNET