I have a c++ dll that connects to a webcam. I am trying to call these frames into a c# wpf application for display.
I have a thread that runs in the dll and sends the data back via a callback, as below: The Updated
function is called every new frame, and updates the zedframe
data;
// class A
//callback from a c++ dll.
public IntPtr zedFrame;
public Mutex mutex = new Mutex();
public bool bGotFrame = false;
public event EventHandler Updated;
protected virtual void Updated(EventArgs e, IntPtr frame)
{
mutex.WaitOne(); // Wait until it is safe to enter.
zedFrame = frame;
mutex.ReleaseMutex(); // Release the Mutex.
bGotFrame = true;
}
In the same class, i have this function that 'should' grab the frame data in a thread safe way.
public IntPtr getFrame()
{
IntPtr tmp;
mutex.WaitOne(); // Wait until it is safe to enter.
tmp = zedFrame;
mutex.ReleaseMutex(); // Release the Mutex.
return tmp;
}
In a wpf form, I have an image:
<Grid>
<Image Name="ImageCameraFrame" Margin="5,5,5,5" MouseDown="GetImageCoordsAt"/>
</Grid>
and in the cs for the form, I have a DispatcherTimer
//in the image form .xaml.cs
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100)
};
timer.Tick += Timer_Tick;
private void Timer_Tick(object sender, EventArgs e)
{
if (ClassA.bGotFrame == true)
{
var data = ClassA.getFrame();
var width = 1280;
var height = 720;
var stride = 3 * width;
var bitmap = BitmapSource.Create(width, height, 96, 96,
PixelFormats.Rgb24, null, data, stride * height, stride);
bitmap.Freeze();
ImageCameraFrame.Source = bitmap;
}
}
When I run this, it connects, shows a single frame, and then crashes the application. I am freezing the bitmap, and using a mutex. What am i doing wrong?
Is it possible to bind the Image source to a variable in another class, updated in another thread? Or do I need the timer?