2
I’m trying to open the Webcam, but every time I try to execute the method iniciarwebcam()
it returns me the error:
There was an exception of type "System.Invalidoperationexception" in System.Windows.Forms.dll, but it was not manipulated in the code of the user Invalid cross-threaded operation: 'Form1' control accessed from a thread that is not the one it was created in.
As I am learning C# now I have to find a solution that fits with mine
Error happens on 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();
};
}
Man, thank you so much! It worked and I don’t have the documentation... I picked up on a tutorial but still, thank you!
– Daniel Souto
look at the @Danielsouto update, I think this second way gets better
– Rovann Linhalis