How To Get A List Of Available Video Capture Devices
Asked Answered
Y

3

3

I am creating a project using DirectShow.Net that shows a preview of a webcam view within a windows form using Visual C#.

I would like to start with gaining a collection of available video devices so I can choose between either the built in webcam or the USB webcam.

I have seen several examples of this being done in C++, e.g. on the msdn "http://msdn.microsoft.com/en-us/library/windows/desktop/dd377566(v=vs.85).aspx".

However as I do not know any C++ I do not know how to convert this code into C#.

Yukoyukon answered 8/10, 2013 at 21:35 Comment(0)
T
8

DirectShow.NET sample \Samples\Capture\DxLogo\Capture.cs shows how to do it:

// Get the collection of video devices
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

The keyword you need is FilterCategory.VideoInputDevice.

See also:

Thirteenth answered 8/10, 2013 at 21:42 Comment(0)
C
6

.netcore solution: Install the package: DirectShowLib.Standard

Then you can get the cameras list:

var devices = new List<DsDevice>(DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice));
var cameraNames = new List<string>();
foreach (var device in devices)
{
    cameraNames.Add(device.Name);
}
Charisecharisma answered 24/6, 2020 at 12:51 Comment(0)
Z
0

I had the same issue. The often suggested approaches of requesting the Windows Media Foundation or Directshow API were not sufficient to me. But fortunately I have found this solution by Michaël Hompus. The usage is as simple as follows:

using var sde = new Hompus.VideoInputDevices.SystemDeviceEnumerator();

foreach (var device in sde.ListVideoInputDevice())
{
    yield return new Webcam
    {
        Name = device.Value,
        Index = device.Key,
    };
}

The respective GitHub repository can be found here.

Zhukov answered 2/4, 2023 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.