Dlaczego wyskakuje mi błąd podczas naciśnięciu buttona w programie skoro po włączeniu go przez plik exe działa prawidłowo?
błąd:
"Cross-thread operation not valid: Control 'label4' accessed from a thread other than the thread it was created on."
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
Bitmap sourceImage = (Bitmap)eventArgs.Frame.Clone();
pictureBox2.Image = ApplyHLSFilter(sourceImage); //applying filter and showing in the picture box
}
private Bitmap ApplyHLSFilter(Bitmap sourceImage)
{
...
objectX = objectRect.X + objectRect.Width / 2 - processedImagen.Width / 2;
objectY = processedImagen.Height / 2 - (objectRect.Y + objectRect.Height / 2);
label4.Text = objectX.ToString(); //błąd
label5.Text = objectY.ToString();
}
return processedImagen;
}
private void button1_Click(object sender, EventArgs e)
{
usbCams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
cam = new VideoCaptureDevice(usbCams[0].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}