1

I'm making a software in C# for College School and I need a help with this:

enter image description here

Youtube tutorial used: http://www.youtube.com/watch?v=CaYQLq72ZN4

an error occurred while capturing the video image. the video capture will now be terminated. Object reference not set to an instance of an object.

The first time executed the software, it worked perfectly, now when I closed and opened again gave this error (it seems that a process is running). I followed a tutorial from youtube, I noticed that there were many complaints because of a similar error. It works yes, but only if you press 3 times the button "start", I have no idea of the error.

CODES:

FRM:

using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace WinApp_FingerControl
{
    public partial class Frm_CadastroCliente : Form
    {

        public Frm_CadastroCliente()
        {
            InitializeComponent();
        }
        WebCam webcam;
        private void Frm_CadastroCliente_Load(object sender, EventArgs e)
        {
            webcam = new WebCam();
            webcam.InitializeWebCam(ref imgVideo);
        }

        private void btn_IniciarCamera_Click(object sender, EventArgs e)
        {
            webcam.Start();
        }

        private void btn_Salvar_Click(object sender, EventArgs e)
        {
            Helper.SaveImageCapture(imgCapture.Image);
        }

        private void btn_FormatoVideo_Click(object sender, EventArgs e)
        {
            webcam.ResolutionSetting();
        }

        private void btnCapturar_Click(object sender, EventArgs e)
        {
            imgCapture.Image = imgVideo.Image;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            webcam.AdvanceSetting();
        }


    }
}

Webcam.cs:

{

    class WebCam
    {
        private WebCamCapture webcam;
        private System.Windows.Forms.PictureBox _FrameImage;
        private int FrameNumber = 30;
        public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
        {
            webcam = new WebCamCapture();
            webcam.FrameNumber = ((ulong)(0ul));
            webcam.TimeToCapture_milliseconds = FrameNumber;
            webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
            _FrameImage = ImageControl;
        }

        void webcam_ImageCaptured(object source, WebcamEventArgs e)
        {
            _FrameImage.Image = e.WebCamImage;
        }

        public void Start()
        {
                webcam.TimeToCapture_milliseconds = FrameNumber;
                webcam.Start(0);
        }

        public void ResolutionSetting()
        {
            webcam.Config();
        }

        public void AdvanceSetting()
        {
            webcam.Config2();
        }

    }
}

Helper.cs

class Helper
{

    public static void SaveImageCapture(System.Drawing.Image image)
    {

        SaveFileDialog s = new SaveFileDialog();
        s.FileName = "Image";// Default file name
        s.DefaultExt = ".Jpg";// Default file extension
        s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

        // Show save file dialog box
        // Process save file dialog box results
        if (s.ShowDialog()==DialogResult.OK)
        {
            // Save Image
            string filename = s.FileName;
            FileStream fstream = new FileStream(filename, FileMode.Create);
            image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            fstream.Close();

        }

    }
}
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Pedro Paulo Amorim
  • 1,434
  • 2
  • 18
  • 41
  • Did you find a way to fix this? – Corne Nov 12 '13 at 07:27
  • Is there any other stack trace information associated with this error? A line number or a method name? Those would be good clues to help focus on where the error is coming from. – StingyJack Feb 24 '14 at 23:46
  • Now I have the same issue. Is there anyone out there who can help ?? – antony.ouseph.k Mar 28 '14 at 07:08
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 17 '14 at 18:51

1 Answers1

-5

Kill the processes of other applications like Skype that use the webcam and your code will run. :)

Adam Lear
  • 35,439
  • 12
  • 80
  • 98