c#使用Aforge调用摄像头拍照的方法

  

本文实例为大家分享了c#使用Aforge调用摄像头拍照的具体代码,供大家参考,具体内容如下

  

<强>一、新建一个Winform项目

  

<强>二、使用Nuget添加引用

  

 C #使用Aforge调用摄像头拍照的方法

  

安装下图中红色框住的两个程序包

  

 C #使用Aforge调用摄像头拍照的方法

  

安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖。

  

 C #使用Aforge调用摄像头拍照的方法

  

<强>三、编写程序

  

1。窗体设计,摄像头是下拉列表(cmbCamera,控件命名,下同),虽然示例只用到一个摄像头,但是该演示可用于多个摄像头间切换场景,分辨率是下拉列表(cmbResolution),列出摄像头所支持的分辨率,一个VideoSourcePlayer控件(vispShoot),一个图片框控件(picbPreview)。

  

 C #使用Aforge调用摄像头拍照的方法

  

2。编写代码

  

        使用系统;   使用System.Collections.Generic;   使用System.ComponentModel;   使用System.Data;   使用System.Drawing;   使用来;   使用text;   使用System.Windows.Forms;   使用AForge.Video.DirectShow;      名称空间AforgeDemo   {   公共部分中类Form1:形式   {   私人FilterInfoCollection videoDevices;   私人VideoCaptureDevice videoDevice;   私人VideoCapabilities [] VideoCapabilities;   私人VideoCapabilities [] snapshotCapabilities;   公共Form1 ()   {   InitializeComponent ();   }      私人空间Form1_Load(对象发送方,EventArgs e)   {   videoDevices=new FilterInfoCollection (FilterCategory.VideoInputDevice);   如果(videoDevices。数!=0)   {   foreach (videoDevices FilterInfo设备)   {   cmbCamera.Items.Add (device.Name);   }   }   其他的   {   cmbCamera.Items.Add(“没有找到摄像头”);   }      cmbCamera。SelectedIndex=0;   }      私人空间cmbCamera_SelectedIndexChanged(对象发送方,EventArgs e)   {   如果(videoDevices。数!=0)   {   videoDevice=new VideoCaptureDevice (videoDevices [cmbCamera.SelectedIndex] .MonikerString);   GetDeviceResolution (videoDevice);   }   }      私人空间GetDeviceResolution (VideoCaptureDevice VideoCaptureDevice)   {   cmbResolution.Items.Clear ();   videoCapabilities=videoCaptureDevice.VideoCapabilities;   foreach (VideoCapabilities VideoCapabilities额)   {   cmbResolution.Items.Add ($ {capabilty.FrameSize”。宽度x} {capabilty.FrameSize.Height}”);   }   cmbResolution。SelectedIndex=0;   }      私人空间btnConnect_Click(对象发送方,EventArgs e)   {   如果(videoDevice !=null)   {   如果((videoCapabilities !=null),,(videoCapabilities。长度!=0))   {   videoDevice。VideoResolution=videoCapabilities [cmbResolution.SelectedIndex];      vispShoot。VideoSource=videoDevice;   vispShoot.Start ();   EnableControlStatus(假);   }   }   }      私人空间EnableControlStatus (bool状态)   {   cmbCamera。启用=状态;   cmbResolution。启用=状态;   btnConnect。启用=状态;   btnShoot。启用=!状态;   btnDisconnect。启用=!状态;   }      私人空间btnDisconnect_Click(对象发送方,EventArgs e)   {   断开();   EnableControlStatus(真正的);   }      私人空间断开()   {   如果(vispShoot。VideoSource !=null)   {   vispShoot.SignalToStop ();   vispShoot.WaitForStop ();   vispShoot。VideoSource=零;   }   }      私人空间btnShoot_Click(对象发送方,EventArgs e)   {   位图img=vispShoot.GetCurrentVideoFrame ();   picbPreview。形象=img;   }      私人空间Form1_FormClosing(对象发送方,FormClosingEventArgs e)   {   断开();   }   }   }   之前      

3。测试

  

 C #使用Aforge调用摄像头拍照的方法

  

附上窗体设计代码:

        名称空间AforgeDemo   {   中部分类Form1   {///& lt; summary>///必需的设计器变量。///& lt;/summary>   私人System.ComponentModel。IContainer组件=零;///& lt; summary>///清理所有正在使用的资源。///& lt;/summary>///& lt;参数name="处理"祝辞如果应释放托管资源,为真的,否则为假。;/param>   保护覆盖空白处理(bool处理)   {   如果处理,,(组件!=null))   {   components.Dispose ();   }   base.Dispose(处理);   }      #地区Windows窗体设计器生成的代码///& lt; summary>///设计器支持所需的方法,不要修改///使用代码编辑器修改此方法的内容。///& lt;/summary>   私人空间InitializeComponent ()   {   这一点。label1=new System.Windows.Forms.Label ();   这一点。cmbCamera=new System.Windows.Forms.ComboBox ();   这一点。label2=new System.Windows.Forms.Label ();   这一点。cmbResolution=new System.Windows.Forms.ComboBox ();   这一点。vispShoot=new AForge.Controls.VideoSourcePlayer ();   这一点。picbPreview=new System.Windows.Forms.PictureBox ();   这一点。btnConnect=new System.Windows.Forms.Button ();   这一点。btnDisconnect=new System.Windows.Forms.Button ();   这一点。btnShoot=new System.Windows.Forms.Button ();   ((System.ComponentModel.ISupportInitialize) (this.picbPreview)) .BeginInit ();   this.SuspendLayout ();////label1//this.label1。AutoSize=true;   this.label1。位置=new System.Drawing。点(39岁,22);   this.label1。Name=" label1”;   this.label1。=new System.Drawing大小。大小(53岁,12);   this.label1。TabIndex=0;   this.label1。文本="摄像头:”;////cmbCamera//this.cmbCamera。FormattingEnabled=true;   this.cmbCamera。位置=new System.Drawing。点(98年,19);   this.cmbCamera。Name=" cmbCamera”;   this.cmbCamera。=new System.Drawing大小。大小(113年,20);   this.cmbCamera。TabIndex=1;   this.cmbCamera。null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

c#使用Aforge调用摄像头拍照的方法