Aforge.net类库调用摄像头拍照(C#)

来源:互联网 发布:ubuntu apt get 换源 编辑:程序博客网 时间:2024/06/05 13:13
官网下载library
 http://www.aforgenet.com/framework/downloads.html


添加引用:

AForge.Imaging ——图像处理和过滤器
AForge.Video ——视频处理类库
AForge.Controls—— 图像显示控件
工具箱添加aforge的组件:

添加组件:

先加载列出来所有可用摄像头放到combobox,从combobox选择要用的摄像头


思路:先列出所有摄像头--打开摄像头--抓拍

using System;using System.Drawing;using System.Windows.Forms;using AForge.Video.DirectShow;namespace Aforge{    public partial class Form1 : DevComponents.DotNetBar.OfficeForm    {        public Form1()        {            InitializeComponent();        }        private FilterInfoCollection videoDevices;        private VideoCaptureDevice videoSource;        private int Indexof = 0;        //添加所有的摄像头到combobox列表里        private void Camlist()        {            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);            if (videoDevices.Count == 0)            {                MessageBox.Show("未找到摄像头设备");            }            foreach (FilterInfo device in videoDevices)            {                Cameralist.Items.Add(device.Name);            }        }        //选择要调用的摄像头,捕获视频并展示到videoSourcePlayer1        private void Device_Click(object sender, EventArgs e)        {            Indexof = Cameralist.SelectedIndex;            if (Indexof < 0)            {                MessageBox.Show("请选择一个摄像头");                return;            }            this.pictureBox1.Visible = false;            this.videoSourcePlayer1.Visible = true;            //videoDevices[Indexof]确定出用哪个摄像头了。            videoSource = new VideoCaptureDevice(videoDevices[Indexof].MonikerString);            //设置下像素,这句话不写也可以正常运行:            videoSource.VideoResolution = videoSource.VideoCapabilities[Indexof];            //在videoSourcePlayer1里显示            videoSourcePlayer1.VideoSource = videoSource;            videoSourcePlayer1.Start();        }        //拍照//这里多了一个pictureBox1,想的是展示下抓拍的照片        private void buttonX1_Click(object sender, EventArgs e)        {            if (videoSource == null)            {                MessageBox.Show("请先打开摄像头");                return;            }            //videoSourcePlayer继承Control父类,定义 GetCurrentVideoFrame能输出bitmap            Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();            pictureBox1.Image = bitmap;            this.videoSourcePlayer1.Visible = false;            this.pictureBox1.Visible = true;            //这里停止摄像头继续工作 当然videoSourcePlayer里也定义了 Stop();用哪个都行            videoSourcePlayer1.Stop();            // videoSourcePlayer1.SignalToStop(); videoSourcePlayer1.WaitForStop();        }        private void buttonX2_Click(object sender, EventArgs e)        { // 加载出来所有的摄像头            Camlist();        }    }}


(新人学习笔记,思路粗劣,让大家见笑了。)