winform 加载AForge.dll中的videoSourcePlayer。

公司最近在做人脸识别,用到了videoSourcePlayer 但是引入了AForge.dll之后,工具箱中并没有videoSourcePlayer,这时就需要自己添加选项卡了。很简单几步就能完成。

首先右键工具箱--->添加选项卡

 

 

 名字随便起,我里就叫AForge,然后选中选项卡右键选择项

 

 

 选择dll,点击OK 

 

 

 再次点击添加到选项卡中

 

 

 就OK了。

然后是控件的使用。将videoSourcePlayer拖到winform中。进入后台代码中

 

 private FilterInfoCollection videoDevices;//获取摄像头private void Form1_Load(object sender, EventArgs e){try{// 枚举所有视频输入设备videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);if (videoDevices.Count == 0)throw new ApplicationException();foreach (FilterInfo device in videoDevices){comboBox1.Items.Add(device.Name);}//下拉框用来更换摄像头comboBox1.SelectedIndex = 0;}catch (ApplicationException){comboBox1.Items.Add("未发现摄像头");comboBox1.SelectedIndex = 0;videoDevices = null;}}

 

 //关闭摄像头 private void button2_Click(object sender, EventArgs e) { if (videoSourcePlayer != null && videoSourcePlayer.IsRunning) { videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); } } //打开摄像头 private void button1_Click(object sender, EventArgs e) { button2_Click(null, null); if (comboBox1.SelectedItem.ToString() == "未发现摄像头") { MessageBox.Show("未发现摄像头", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); videoSource.VideoResolution = videoSource.VideoCapabilities[comboBox2.SelectedIndex]; videoSourcePlayer.VideoSource = videoSource; videoSourcePlayer.Start(); }

 //换摄像头 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedItem.ToString() == "未发现摄像头") { comboBox2.Items.Add("未发现摄像头"); comboBox2.SelectedIndex = 0; return; } VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); if (videoSource.VideoCapabilities.Count() == 0) { comboBox2.Items.Add("摄像头异常"); comboBox2.SelectedIndex = 0; return; } comboBox2.Items.Clear(); foreach (AForge.Video.DirectShow.VideoCapabilities FBL in videoSource.VideoCapabilities) { comboBox2.Items.Add(FBL.FrameSize.Width + "*" + FBL.FrameSize.Height); } comboBox2.SelectedIndex = 0; button1_Click(null, null); }

总共就4个方法,首先获取摄像头,然后 打开摄像头 ,关闭摄像头,在更换摄像头中有获取分辨率的方法,这样就能打摄像头并在videoSourcePlayer中看到视频了

需要获取当前画面,就调用

Bitmap bitmap = videoSourcePlayer.GetCurrentVideoFrame();

基本上在videoSourcePlayer的方法中都有,就不赘叙了。

相关文章