1.打开VS,新建一个Form窗体,工具->NuGet包管理工具->管理解决方案的NuGet包,在浏览里搜索AForge.Controls、AForge.Video.DirectShow,安装AForge.Controls和AForge.Video.DirectShow
2.安装AForge组件完成后,VS工具箱会新增AForge控件,把AForge.NET中的VideoSourcePlayer拖到Form窗体上
3.关键代码
#region 变量 /// <summary> /// 摄像头设备集合 /// </summary> FilterInfoCollection videoDevices; /// <summary> /// 捕获设备资源 /// </summary> VideoCaptureDevice videoSource; /// <summary> /// 处理图片 /// </summary> Bitmap bitImg; #endregion /// <summary> /// 先检测摄像头 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { //检测电脑所有的摄像头 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); MessageBox.Show("检测到了【" + videoDevices.Count.ToString() + "】个摄像头"); //获取需要拍照的设备 videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); videoSourcePlayer1.VideoSource = videoSource; //启动摄像头 videoSourcePlayer1.Start(); } /// <summary> /// 拍照 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { //拍摄并获取图片 bitImg = videoSourcePlayer1.GetCurrentVideoFrame(); } /// <summary> /// 关闭摄像头 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { ShutCamera(); } /// <summary> /// 关闭并释放摄像头 /// </summary> public void ShutCamera() { if (videoSourcePlayer1.VideoSource != null) { videoSourcePlayer1.SignalToStop(); videoSourcePlayer1.WaitForStop(); videoSourcePlayer1.VideoSource = null; } }
标签:拍照,C#,void,AForge,videoDevices,videoSourcePlayer1,摄像头 From: https://www.cnblogs.com/mengzhixingping/p/18182007