Hi, i am capturing image with Webcam. based on the device(laptop) used, the image capture either freezes at some point or not. What i want to achieve is to ensure that the capture does not freeze.
Here is the function that captures the image. it uses Direct Show Library
private void captureImage()
{
if (m_ip != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(m_ip);
m_ip = IntPtr.Zero;
}
m_ip = cam.Click();
b = new Bitmap(cam.Width, cam.Height, cam.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, m_ip);
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
System.Windows.Controls.Image win = UIHelper.FindChild<System.Windows.Controls.Image>(System.Windows.Application.Current.MainWindow, "photo");
win.Source = BitmapToImageSource(b);
cam.Dispose();
this.DialogResult = true;
this.Close();
}
What I have tried:
i'm trying to implement the asynchronous operation in a the Tick event handler of a dispatcher timer
void timer_Tick(object sender, EventArgs e)
{
i = --i;
count.Content = i;
if (i == 2)
{
prog.IsActive = true;
new Task(captureImage).Start();
}
if (i == 0)
{
timer.Stop();
}
}
when i do that, i get this error "The calling thread cannot access this object because a different thread owns it" on this line "
System.Windows.Controls.Image win = UIHelper.FindChild<System.Windows.Controls.Image>(System.Windows.Application.Current.MainWindow, "photo");
" in the capture function
but when i use a dispatcher.Invoke like this
this.Dispatcher.Invoke((Action)(() =>
{
captureImage();
}));
the frame still freezes, any idea how to fix this?