I've seen a couple of solutions for this but they don't seem to use eventargs. So, in Windows Forms, a working code:
private void StartButton_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[cboDevices.SelectedIndex].MonikerString);
FinalFrame.NewFrame += FinalFrame_NewFrame;
FinalFrame.Start();
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pboLive.Image = (Bitmap)eventArgs.Frame.Clone();
}
But in WPF:
private void StartButton_Click(object sender, RoutedEventArgs e){ int capturedeviceindex = cboDevices.SelectedIndex; FilterInfo cd = CaptureDevice[cboDevices.SelectedIndex]; string cdms = cd.MonikerString; FinalFrame = new VideoCaptureDevice(cdms); FinalFrame.NewFrame += FinalFrame_NewFrame; FinalFrame.Start(); }
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs){ pboLive.Source = ImageSourceForBitmap((Bitmap)eventArgs.Frame.Clone());}
not working due to
"The calling thread cannot access this object because a different thread owns it."
I'm almost 100% sure I am somehow supposed to use
this.Dispatcher.Invoke(() => {
...// your code here.});
but how does that go with eventargs?
OK, so the current answer I marked works once for some reason, then it requires restarting the program.
Is there some need to explicitly destroy some threads that are left alive after the WPF window is closed (while the Word application from where the program is called is still alive)?
On subsequent times it gets in the code of:
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) { var imageSource = ImageSourceForBitmap(eventArgs.Frame); imageSource.Freeze(); pboLive.Dispatcher.Invoke(() => pboLive.Source = imageSource); }
to that last row of "Dispatcher.Invoke..." and seems to be in infinite loop without displaying anything on pboLive
edit. Sorry for the code formatting. Line changes doesn't seem to be allowed here, don't ask me why.