vendredi 18 avril 2014

objet - c# erreur de webcam - Stack Overflow


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();

}

}
}

Thanks! :D



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();

}

}
}

Thanks! :D


0 commentaires:

Enregistrer un commentaire