I'm trying to open the Webcam, however every time I try to execute the iniciarwebcam()
method it returns me the error:
An exception of type "System.InvalidOperationException" occurred in System.Windows.Forms.dll, but it has not been manipulated in Invalid Interstring Operation: Control 'Form1' accessed from a thread that is not the one in which it was created.
As I'm learning C # right now with difficulties of finding a solution that fit my
The error happens on the line:
video.NewFrame += (s, b) => pictureBox1.Image = (Bitmap)b.Frame.Clone();
Code:
using AForge.Video.DirectShow;
public void inciarwebcam()
{
FilterInfoCollection filtro = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (filtro != null)
{
video = new VideoCaptureDevice(filtro[0].MonikerString);
video.NewFrame += (s, b) => pictureBox1.Image = (Bitmap)b.Frame.Clone();
video.Start();
}
}
public void fecharwebcam()
{
if (video != null && video.IsRunning ) {
video.SignalToStop();
video = null;
}
}
public VideoCaptureDevice video;
private void button6_Click(object sender, EventArgs e)
{
if (button6.Text == "Desativado")
{
button6.Text = "Ativado";
button6.BackColor = Color.Green;
ard.Open();
inciarwebcam();
}
else
{
button6.BackColor = Color.DarkRed;
button6.Text = "Desativado";
ard.Close();
fecharwebcam();
};
}