Aforge.net framework采集摄像头视频

C#使用Aforge.net framework采集摄像头视频

(2010-08-20 22:24:32)
转载
标签:

c

aforge

摄像头

it

 

对于视频编程,网络上的东西不是很好找,摄像头算是比较初级的东东了,我查了很多资料,才算是有一点结果,现在把摄像头采集程序代码与大家分享一下。(vs2008编译通过)

其中,Aforge.net下载地址http://www.aforgenet.com/framework/

下载lib之后,引用就可以了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;


namespace 视频
{
    publicpartial class Form1 : Form
    {
       private bool DeviceExist = false;
       private FilterInfoCollection videoDevices;
       private VideoCaptureDevice videoSource = null;
       private Timer timer1 = new Timer();
       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           getCamList();
       }

       private voidgetCamList()       
       {           
           try           
           {               
               videoDevices = newFilterInfoCollection(FilterCategory.VideoInputDevice);               
               comboBox1.Items.Clear();              
               if (videoDevices.Count ==0)                  
                   throw newApplicationException();              
               DeviceExist =true;              
               foreach (FilterInfo device invideoDevices)              
               {                  
                   comboBox1.Items.Add(device.Name);            
               }              
               comboBox1.SelectedIndex = 0; //make dafault to firstcam          
           }          
           catch(ApplicationException)           
           {               
               DeviceExist =false;             
               comboBox1.Items.Add("No capture device on yoursystem");       
           }     
       }

       private void CloseVideoSource()
       {
           if (!(videoSource ==null))           
               if (videoSource.IsRunning)
               {
                   videoSource.SignalToStop();
                   videoSource = null;
               }
       }

       private void video_NewFrame(object sender, NewFrameEventArgseventArgs)       
       {          
           Bitmap img =(Bitmap)eventArgs.Frame.Clone();           //do processinghere           
           pictureBox1.Image =img;   
           //pictureBox1.Image.
       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           label2.Text = "Device running... " +videoSource.FramesReceived.ToString() + " FPS";
       }

       private void Form1_FormClosed(object sender, FormClosedEventArgse)
       {
           CloseVideoSource();
       }

       private void btnPlay_Click(object sender, EventArgs e)
       {
           if (DeviceExist)
           {
               videoSource = newVideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
               videoSource.NewFrame += newNewFrameEventHandler(video_NewFrame);
               CloseVideoSource();
               videoSource.DesiredFrameSize = new Size(pictureBox1.Width,pictureBox1.Height);
               //videoSource.DesiredFrameSize = new Size(353, 290);
               //videoSource.DesiredFrameRate =10;                 
               videoSource.Start();
               label2.Text = "Device running...";
               timer1.Enabled = true;
           }
           else
           {
               label2.Text = "Error: No Device selected.";
           }
       }

       private void btnStop_Click(object sender, EventArgs e)
       {
           if (videoSource.IsRunning)
           {
               timer1.Enabled = false;
               CloseVideoSource();
               label2.Text = "Device stopped.";
           }
       }

       private void btnExit_Click(object sender, EventArgs e)
       {
           CloseVideoSource();
           this.Dispose();
       }

    }
}

Aforge.net framework采集摄像头视频_第1张图片

这样,一个简单的摄像头视频程序便可以使用了。


你可能感兴趣的:(编程,timer,object,video,null,Class)