尝试使用Aforge保存视频时出现空引用异常

时间:2015-12-29 11:15:44

标签: c# aforge

我正在尝试使用Microsoft Lifecam开发一个winform应用程序,我需要使用网络摄像头捕获视频并尝试使用Aforge将视频保存到文件夹。

构建编译成功但每次运行应用程序时。它会抛出错误

  

"未处理的类型' System.NullReferenceException'   发生在System.Drawing.dll

中      

附加信息:对象引用未设置为的实例   。对象"

我无法弄清楚弹出此错误的原因。

完整的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Video.VFW;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.LifeCam.Controllers;
using Microsoft.LifeCam.WebCam;
using Microsoft.LifeCam.Framework;
using Microsoft.LifeCam.Application;
using Microsoft.LifeCam.Interfaces;
using Microsoft.LifeCam;


namespace webcam_application
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection integratedcamera;
        private VideoCaptureDevice capturedevice;
        private VideoCaptureDeviceForm capturedevice1;
        private VideoCaptureDevice final_video = new VideoCaptureDevice();
        private VideoFileWriter FileWriter = new VideoFileWriter();
        private SaveFileDialog saveavi;

        private Bitmap video;

        public Bitmap Video
        {
            get
            {
                return video;
            }

            set
            {
                video = value;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

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

            comboBox1.SelectedIndex = 0;
            capturedevice = new VideoCaptureDevice();
            capturedevice1 = new VideoCaptureDeviceForm();

            capturedevice = new VideoCaptureDevice(integratedcamera[comboBox1.SelectedIndex].MonikerString);
            capturedevice.NewFrame += new NewFrameEventHandler(Finalframe_Newframe);
            capturedevice.Start();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            closecamera();
        }

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

        }

        void Finalframe_Newframe(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap video = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = video;

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            closecamera();
        }

        void closecamera()
        {
            if (capturedevice != null)
            {
                if (capturedevice.IsRunning == true)
                {
                    capturedevice.Stop();
                    pictureBox1.Image = new Bitmap(640, 480);

                }
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (capturedevice1.ShowDialog(this) == DialogResult.OK)
            {
                VideoCaptureDevice videoSource = capturedevice1.VideoDevice;
                final_video = capturedevice1.VideoDevice;
                final_video.NewFrame += Final_video_NewFrame;
                final_video.Start();
            }



        }

        private void Final_video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            FileWriter.WriteVideoFrame(video);

        }


        private void button3_Click(object sender, EventArgs e)
        {

            saveavi = new SaveFileDialog();
            saveavi.Filter = "Avi Files (*.avi)|*.avi";
            if (saveavi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int h = capturedevice1.VideoDevice.VideoResolution.FrameSize.Height;
                int w = capturedevice1.VideoDevice.VideoResolution.FrameSize.Width;
                FileWriter.Open(saveavi.FileName, w, h, 25, VideoCodec.Default, 5000000);
                // Bitmap abc = new Bitmap(video);
                FileWriter.WriteVideoFrame(video);

                if (final_video.IsRunning)
                {
                    FileWriter.Close();
                    final_video.SignalToStop();
                    //this.FinalVideo.Stop();
                    //this.AVIwriter.Close();
                    pictureBox1.Image = null;
                }

            }
            }
        }

    }

0 个答案:

没有答案