Aforge用于摄像头

时间:2013-04-03 11:22:23

标签: c# aforge

我写了一个代码,用于通过两个按钮和一个图片框来访问网络摄像头

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.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;

namespace cam
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }
    private FilterInfoCollection webcam;
    private VideoCaptureDevice cam;
    Bitmap bitmap;

    private void Form1_Load(object sender, EventArgs e)
    {
        webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo VideoCaptureDevice in webcam)
        {
            comboBox1.Items.Add(VideoCaptureDevice.Name);

        }
        comboBox1.SelectedIndex = 0;
      }

    private void button1_Click(object sender, EventArgs e)
    {
        cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
        cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
        cam.Start();

    }
    void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();

        pictureBox1.Image = bitmap;
    }



    private void button3_Click(object sender, EventArgs e)
    {
        if (cam.IsRunning)
        {
            cam.Stop();
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = bitmap;
    }



    }

 }

代码成功构建。但在调试时,图片框无效。启动和停止工作正常。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

你必须先删除

Bitmap bitmap;

因为它的null和你在图片框中显示的内容 不是来自相机的图片

答案 1 :(得分:0)

很久以前我也遇到了aforge的问题但是让它运转起来了 请参阅我的代码:How initialize AForge webcam