C# source code for using USB camera in WinForms/WPF.
With only single CSharp source code. No external library required.
Add UsbCamera.cs to your project.
Following is how to use. see also SampleProject.
// [How to use]
// check USB camera is available.
string[] devices = UsbCamera.FindDevices();
if (devices.Length == 0) return; // no camera.
// get video format.
var cameraIndex = 0;
var formats = UsbCamera.GetVideoFormat(cameraIndex);
// select the format you want.
foreach (var item in formats) Console.WriteLine(item);
// for example, video format is like as follows.
// 0:[Video], [MJPG], {Width=1280, Height=720}, 333333, [VideoInfo], ...
// 1:[Video], [MJPG], {Width=320, Height=180}, 333333, [VideoInfo], ...
// 2:[Video], [MJPG], {Width=320, Height=240}, 333333, [VideoInfo], ...
// ...
var format = formats[0];
// create instance.
var camera = new UsbCamera(cameraIndex, format);
// this closing event handler make sure that the instance is not subject to garbage collection.
this.FormClosing += (s, ev) => camera.Release(); // release when close.
// show preview on control.
camera.SetPreviewControl(pictureBox1.Handle, pictureBox1.ClientSize);
pictureBox1.Resize += (s, ev) => camera.SetPreviewSize(pictureBox1.ClientSize); // support resize.
// start.
camera.Start();
// get image.
// Immediately after starting the USB camera,
// GetBitmap() fails because image buffer is not prepared yet.
var bmp = camera.GetBitmap();
By default, GetBitmap() returns image of System.Drawing.Bitmap.
If WPF, define 'USBCAMERA_WPF' symbol that makes GetBitmap() returns image of BitmapSource.
SetPreviewControl requires window handle but WPF control does not have handle.
it is recommended to use PictureBox with WindowsFormsHost.
var handle = pictureBox.Handle;
camera.SetPreviewControl(handle, new Size(320, 240));
or use
var handle = new System.Windows.Interop.WindowInteropHelper(this).Handle;
camera.SetPreviewControl(handle, new Size(320, 240));
or use this conventional way. (works little heavy)
var timer = new System.Timers.Timer(1000 / 30);
timer.Elapsed += (s, ev) => Dispatcher.Invoke(() => image.Source = camera.GetBitmap());
timer.Start();
this.Closing += (s, ev) => timer.Stop();
Some cameras can produce a still image separate from the capture stream,
and often the still image is of higher quality than the images produced by the capture stream.
If your camera has a hardware button, your camera might supports still image capture. Push it, or call StillImageTrigger().
When you capture still image, you receive StillImageCaptured call back.
if (camera.StillImageAvailable)
{
button.Click += (s, ev) => camera.StillImageTrigger();
camera.StillImageCaptured += bmp => pictureBox.Image = bmp;
}
// adjust properties.
UsbCamera.PropertyItems.Property prop;
prop = camera.Properties[DirectShow.CameraControlProperty.Exposure];
if (prop.Available)
{
// get current value
var val = prop.GetValue();
// set new value
var min = prop.Min;
var max = prop.Max;
var def = prop.Default;
var step = prop.Step;
prop.SetValue(DirectShow.CameraControlFlags.Manual, def);
}
prop = camera.Properties[DirectShow.VideoProcAmpProperty.WhiteBalance];
if (prop.Available && prop.CanAuto)
{
prop.SetValue(DirectShow.CameraControlFlags.Auto, 0);
}
This project make use of a part of source code of the project below. Thank you!
- DShowLib (author: Mr. M.Oshikiri)
- DSLab (author: cogorou)
You can make H264 recorder by using UsbCamera and OpenH264Lib.NET and AviWriter(in MotionJPEGWriter).
See README.md in OpenH264Lib.NET.
You can make MotionJPEG movie with audio, see README.md in AudioIO.