using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AForge; using AForge.Controls; using AForge.Neuro; using AForge.Video; using AForge.Video.DirectShow; using static recharge.Form1; namespace recharge { public partial class Form1 : Form { FilterInfoCollection videoDevices;//摄像头设备集合 VideoCaptureDevice videoSource;//捕获设备源 public int selctedDeviceIndxe = 0; Bitmap img;//处理图片 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //先检测电脑所有的摄像头 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); MessageBox.Show("检测到了" + videoDevices.ToString() + "个摄像头!"); } // CS0021.cs // compile with: /reference:CPP0021.dll private void pictureBox1_Click(object sender, EventArgs e) { } private void videoSourcePlayer1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); selctedDeviceIndxe = 0; videoSource = new VideoCaptureDevice(videoDevices[selctedDeviceIndxe].MonikerString); videoSource.VideoResolution = videoSource.VideoCapabilities[selctedDeviceIndxe]; videoSourcePlayer1.VideoSource = videoSource; videoSourcePlayer1.Start(); img = videoSourcePlayer1.GetCurrentVideoFrame(); } } }
时间: 2024-02-14 17:15:32 浏览: 154
libemgucv-windesktop-3.4.1.2976.exe
根据您提供的代码,问题出现在 `videoSource = new VideoCaptureDevice(videoDevices[selctedDeviceIndxe].MonikerString);` 这一行,因为 `videoDevices` 是一个 `FilterInfoCollection` 类型的对象,而这个类型不支持通过方括号进行索引操作。
要解决这个问题,您可以使用 `videoDevices[selctedDeviceIndxe]` 的返回值来获取当前选中的 `FilterInfo` 对象,然后再使用它的 `MonikerString` 属性来创建 `VideoCaptureDevice` 对象。修改后的代码如下:
```
FilterInfo selectedDevice = videoDevices[selctedDeviceIndxe];
videoSource = new VideoCaptureDevice(selectedDevice.MonikerString);
videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDevice];
```
这样就可以避免使用带有方括号的索引操作了。希望这可以帮助到您!
阅读全文