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

215 篇文章 2 订阅
25 篇文章 6 订阅

作者:李举锋

转自:http://blog.sina.com.cn/s/blog_4ababada0100l5ir.html


对于视频编程,网络上的东西不是很好找,摄像头算是比较初级的东东了,我查了很多资料,才算是有一点结果,现在把摄像头采集程序代码与大家分享一下。(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();
       }

    }
}

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

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值