C#使用Aforge调用摄像头拍照的实现附源码
本文主要实现了利用Aforge调用摄像头拍照的实现,源码力求简洁,希望能够帮助到大家
一.新建一个winform应用
二.使用Nuget添加引用
安装下图中红色框住的两个程序包
安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖。
三、编写程序
1.窗体设计,三个button控件(button1 打开摄像头,button2 关闭摄像头,button 3 抓拍),一个VideoSourcePlayer控件,一个PictureBox控件。(界面如图所示)
2.编写源码
using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Video.DirectShow;
namespace PictureboxTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
FilterInfoCollection vd1;
VideoCaptureDevice vd2;
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 打开摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
vd1 = new FilterInfoCollection(FilterCategory.VideoInputDevice);
vd2 = new VideoCaptureDevice(vd1[0].MonikerString);
videoSourcePlayer1.VideoSource = vd2;
videoSourcePlayer1.Start();
}
/// <summary>
/// 关闭摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Stop();
}
/// <summary>
/// 抓拍功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Bitmap bl = videoSourcePlayer1.GetCurrentVideoFrame();
pictureBox1.Image = bl;
}
}
}
3.程序效果
4窗体源码
namespace PictureboxTest
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.videoSourcePlayer1 = new AForge.Controls.VideoSourcePlayer();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(35, 375);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "打开摄像头";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// videoSourcePlayer1
//
this.videoSourcePlayer1.Location = new System.Drawing.Point(12, 60);
this.videoSourcePlayer1.Name = "videoSourcePlayer1";
this.videoSourcePlayer1.Size = new System.Drawing.Size(399, 285);
this.videoSourcePlayer1.TabIndex = 2;
this.videoSourcePlayer1.Text = "videoSourcePlayer1";
this.videoSourcePlayer1.VideoSource = null;
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(435, 60);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(448, 285);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(137, 375);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 4;
this.button2.Text = "关闭摄像头";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(242, 375);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 5;
this.button3.Text = "抓拍";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(895, 427);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.videoSourcePlayer1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "摄像头程序";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private AForge.Controls.VideoSourcePlayer videoSourcePlayer1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}