今天开始使用 aforge库对摄像头的操作进行封装。涉及的操作有:打开摄像头,拍照,关闭摄像头。
我把这些操作封装到一个DLL中。
1, 添加引用AFORGE库的DLL:
AForge.Video.dll;
AForge.Video.DirectShow.dll;
添加名称空间
using AForge.Video; using AForge.Video.DirectShow;
2,添加引用:这个是系统有的,在添加引用的 .net 下面
system.drawing.dll;
添加名称空间:
using System.Drawing;
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 Webcam; using AForge.Controls; using AForge.Video; using AForge.Video.DirectShow; namespace WindowsFormsApplication2 { public partial class Form1 : Form { private VideoCaptureDevice video; private Bitmap bitmap; public Form1() { InitializeComponent(); } /// <summary> /// ---窗口加载函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { string[] videoDevicesName = WebcamHelper.VideoDevicesName(); foreach (string item in videoDevicesName) { comboBox1.Items.Add(item); } comboBox1.SelectedIndex = 0; } /// <summary> /// ---连接摄像头 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void connBtn_Click(object sender, EventArgs e) { video = WebcamHelper.ConnectWebam(comboBox1.SelectedIndex); video.NewFrame += new NewFrameEventHandler(Cam_NewFrame); } ///------自定义函数,捕获每一帧图像并显示 private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs) { bitmap = (Bitmap)eventArgs.Frame.Clone(); pictureBox1.Image = bitmap; ///---回收资源 GC.Collect(); } /// <summary> /// ---断开连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void closeBtn_Click(object sender, EventArgs e) { WebcamHelper.DisConnectWebcam(ref video); } /// <summary> /// --窗口关闭函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (video != null) { ///---关闭摄像头 if (video.IsRunning) { video.Stop(); } } } /// <summary> /// ---拍照函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void captureBtn_Click(object sender, EventArgs e) { ///---定义图片的路径 ///---设置图像的名称和格式。 string filepath =Application.StartupPath + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".jpg"; if (true == WebcamHelper.SaveCapturePicture(ref bitmap, filepath, 200, 100)) { MessageBox.Show("图片保存成功"); } else { MessageBox.Show("图片保存失败"); } } } }
1,添加引用:自己写的DLL(Webcam.dll)
2,源码:
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 Webcam; using AForge.Video; using AForge.Video.DirectShow; namespace WindowsFormsApplication2 { public partial class Form1 : Form { private VideoCaptureDevice video; private Bitmap bitmap; public Form1() { InitializeComponent(); } /// <summary> /// ---窗口加载函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { string[] videoDevicesName = WebcamHelper.VideoDevicesName(); foreach (string item in videoDevicesName) { comboBox1.Items.Add(item); } comboBox1.SelectedIndex = 0; } /// <summary> /// ---连接摄像头 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void connBtn_Click(object sender, EventArgs e) { video = WebcamHelper.ConnectWebam(comboBox1.SelectedIndex); video.NewFrame += new NewFrameEventHandler(Cam_NewFrame); } ///------自定义函数,捕获每一帧图像并显示 private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs) { bitmap = (Bitmap)eventArgs.Frame.Clone(); pictureBox1.Image = bitmap; ///---回收资源 GC.Collect(); } /// <summary> /// ---断开连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void closeBtn_Click(object sender, EventArgs e) { WebcamHelper.DisConnectWebcam(ref video); } /// <summary> /// --窗口关闭函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (video != null) { ///---关闭摄像头 if (video.IsRunning) { video.Stop(); } } } /// <summary> /// ---拍照函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void captureBtn_Click(object sender, EventArgs e) { ///---定义图片的路径 ///---设置图像的名称和格式。 string filepath =Application.StartupPath + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".jpg"; if (true == WebcamHelper.SaveCapturePicture(ref bitmap, filepath, 200, 100)) { MessageBox.Show("图片保存成功"); } else { MessageBox.Show("图片保存失败"); } } } }