Enumerate and select video capture devices
Products: Video Capture SDK .Net
Engines:
VideoCaptureCore
,VideoCaptureCoreX
Video Capture SDK .Net can use any video capture device supported by OS. The code sample shows you how to list available video capture devices and choose one of them.
Enumerate video capture devices
Sample code to enumerate video capture devices and add them to a combobox:
- VideoCaptureCore
- VideoCaptureCoreX
foreach (var device in VideoCapture1.Video_CaptureDevices())
{
cbVideoInputDevice.Items.Add(device.Name);
}
var devices = DeviceEnumerator.Shared.VideoSourcesAsync();
foreach (var device in await devices)
{
cbVideoInputDevice.Items.Add(device.DisplayName);
}
Enumerate available video formats and frame rates
After getting the device name, you can enumerate available video formats and frame rates:
- VideoCaptureCore
- VideoCaptureCoreX
// Find device by name
var deviceItem = VideoCapture1.Video_CaptureDevices().FirstOrDefault(device => device.Name == "Some device name");
// Enumerate video formats
foreach (var format in deviceItem.VideoFormats)
{
cbVideoInputFormat.Items.Add(format);
// Enumerate frame rates for a specified format
foreach (var frameRate in format.FrameRates)
{
cbVideoInputFrameRate.Items.Add(frameRate.ToString(CultureInfo.CurrentCulture));
}
}
// Find device by name
var deviceItem = (await DeviceEnumerator.Shared.VideoSourcesAsync()).FirstOrDefault(device => device.DisplayName == "Some device name");
foreach (var format in deviceItem.VideoFormats)
{
// Add the format to the combobox
cbVideoInputFormat.Items.Add(format.Name);
foreach (var frameRate in format.FrameRateList)
{
// Add the frame rate to the combobox
cbVideoInputFrameRate.Items.Add(frameRate.ToString());
}
}
Set the video capture device as a source
- VideoCaptureCore
- VideoCaptureCoreX
// Find the device by name
var deviceItem = VideoCapture1.Video_CaptureDevices().FirstOrDefault(device => device.Name == "Some device name");
// Create the video capture device
VideoCapture1.Video_CaptureDevice = new VideoCaptureSource(deviceItem.Name);
// Set the default format and frame rate
VideoCapture1.Video_CaptureDevice.Format = deviceItem.VideoFormats[0].ToString();
VideoCapture1.Video_CaptureDevice.FrameRate = deviceItem.VideoFormats[0].FrameRates[0];
Use the VideoPreview
or VideoCapture
mode to preview or capture video from the device.
// Define the video capture device source settings
VideoCaptureDeviceSourceSettings videoSourceSettings = null;
// Find the device by name
var device = (await DeviceEnumerator.Shared.VideoSourcesAsync()).FirstOrDefault(x => x.DisplayName == deviceName);
if (device != null)
{
// Find the format by name
var formatItem = device.VideoFormats.FirstOrDefault(x => x.Name == format);
if (formatItem != null)
{
// Create the video capture device source settings
videoSourceSettings = new VideoCaptureDeviceSourceSettings(device)
{
Format = formatItem.ToFormat()
};
// Set the frame rate
videoSourceSettings.Format.FrameRate = new VideoFrameRate(Convert.ToDouble(cbVideoInputFrameRate.Text));
}
}
// Set the video source
VideoCapture1.Video_Source = videoSourceSettings;
Visit our GitHub page to get more code samples.