对于要进行图像处理的第一步,就是要拍下图片。先进行文档学习!
AForge.Video和AForge.Video.DirectShow 这二个命名空间包含控制视频拍图片的类及接口。
了解基本框架后,怎样拍图?
方法一:1.简单采用标准选择界面 VideoCaptureDeviceForm,选择要使用设备--相机或WEBCAM,得到基本参数2.建立
VideoCaptureDevice对象,再采用其基本类进行拍照,显示视频。
基本代码:
VideoCaptureDeviceForm video_sel_form=new VideoCaptureDeviceForm();
VideoCaptureDevice videoSource = new VideoCaptureDevice( video_sel_form.VideoDeviceMoniker );
videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame );
videoSource.Start( );
private void video_NewFrame( object sender, NewFrameEventArgs eventArgs ) {Bitmap bitmap = eventArgs.Frame; //可以显示,也可以保存等 }
方法二:1.采用遍历视频输入设备给出信息列表。2.建立VideoCaptureDevice对象,再采用其基本类进行拍照,显示视频。此方法可以同时建立几个视频源。
基本代码:
FilterInfoCollection videoDevices = new FilterInfoCollection( FilterCategory.VideoInputDevice );
VideoCaptureDevice videoSource = new VideoCaptureDevice( videoDevices[0].MonikerString );
后面其他同方法一:
案例如图:
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;
using AForge.Video.DirectShow;
namespace AForge_test
{
public partial class Form1 : Form{
public Form1()
{
InitializeComponent();
}
Bitmap bitmap = null;
VideoCaptureDevice videoSource;
private void Form1_Load(object sender, EventArgs e)
{
///方法一,红色部分
// VideoCaptureDeviceForm video_sel_form = new VideoCaptureDeviceForm();
// video_sel_form.ShowDialog();
// videoSource = new VideoCaptureDevice(video_sel_form.VideoDeviceMoniker);
// videoSource = new VideoCaptureDevice(video_sel_form.VideoDeviceMoniker);
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(videoSource_SnapshotFrame);
videoSource.Start();
}
private void videoSource_SnapshotFrame(object sender, NewFrameEventArgs eventArgs)
{
bitmap = (Bitmap)eventArgs.Frame.Clone(); //注意一定采用克隆,否则会无法看得到,完成此此动作后会自动清理
pictureBox1.Image = bitmap;
}
private void button2_Click(object sender, EventArgs e) //抓图
{
Bitmap bitmap2 = bitmap;
pictureBox2.Image = bitmap2;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
videoSource.Stop();
}
}