一、新建一个Windows窗体应用程序,在Form1窗体上添加一个PictureBox控件、一个ComboBox控件,命名为PictureBox1、cbCapture,还有两个Button控件,Text分别为切换和摄像头属性,Name分别为btnStart和btnConfig,其界面如下:

二、在该项目下的“引用”处右击选择“添加引用”,添加 AForge.Video.dll 和 AForge.Video.DirectShow.dll 两个程序集

三、双击两个Button按钮以及触发窗体的Load和Closing事件,代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video;
using System.Diagnostics; namespace Aforge调用摄像头
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//枚举所有的视频输入设备
if (videoDevices.Count == )
throw new ApplicationException();
foreach (FilterInfo device in videoDevices)
{
cbCapture.Items.Add(device.Name);//把所有的视频设备添加到下拉框中
}
cbCapture.SelectedIndex = ;
videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//摄像头的名称
videoSource.DesiredFrameSize = new Size(, );//设置大小
videoSource.DesiredFrameRate = ;//设置帧率
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
private void btnStart_Click(object sender, EventArgs e)
{
videoSource.Stop();
videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//摄像头的名称
videoSource.DesiredFrameSize = new Size(, );//设置大小
videoSource.DesiredFrameRate = ;//设置帧率
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.Start();
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bitmap;
} private void btnConfig_Click(object sender, EventArgs e)
{
if ((videoSource != null) && (videoSource is VideoCaptureDevice))
{
try
{
((VideoCaptureDevice)videoSource).DisplayPropertyPage(this.Handle);
}
catch (NotSupportedException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Process p = Process.GetCurrentProcess();
if (p.HasExited == false)
{
p.Kill();
}
}
}
}

注意:如果在关闭窗体是不把这个进程给结束掉,那么下次再次调用的时候就会出现程序正在运行中的错误或者在窗体加载时不能调用处摄像头;此外,摄像头必须是无驱的

四、效果图

最新文章

  1. error LNK2019 无法解析的外部符号 __imp__accept@12
  2. FireDac 与数据库连接时字符集及对应的字段类型问题
  3. Java_注解_01_注解(Annotation)详解
  4. Js DOM 详解
  5. SpringBoot入门:Spring Data JPA 和 JPA(理论)
  6. Linux的sleep()和usleep()
  7. 水晶报表中"已达到系统管理员配置的最大报表处理作业数限制"错误的处理
  8. 查表法解决calendar中月份及星期初始值为0的情况。
  9. js字符串String提取方法比较
  10. 《程序猿闭门造车》之NBPM工作流引擎 - 项目整体架构
  11. 【22】访问者模式(Visitor Pattern)
  12. cookie之三天免登录代码
  13. 解决:springmvc中接收date数据问题
  14. python-flask-SQLAlchemy
  15. mysql数据库主从同步读写分离(一)主从同步
  16. 其他ip无法访问Yii的gii,配置ip就可以
  17. kill -9 a postgres process
  18. mysql数据库从删库到跑路之mysql库操作
  19. Camera & Render
  20. django使用celery实现异步操作

热门文章

  1. 《ASP.NET MVC 5 破境之道》:第一境 ASP.Net MVC5项目初探 — 第三节:View层简单改造
  2. hdu 4325 Flowers(区间离散化)
  3. 20164317《网络对抗技术》Exp1 PC平台逆向破解
  4. openvswitch 驱动卸载失败(Module openvswitch is in use)
  5. ModelForm错误验证自定义钩子和全局钩子
  6. hello lua
  7. Django(wsgiref、jinja2模块使用介绍)
  8. MySQL远程登陆解决
  9. my SQL Workbench创建数据库
  10. 结合 RunLoop 和 Instrument 定位卡顿