I am creating a software in C#/XAML which display a video from a camera.
For each frame of my video I have an OpenCV image, I manipulate the image (add circles ...) and then display the video to the user.
In XAML I use the image object to display the video. The image object is linked to a WriteableBitmap, I copy my opencv image into the WriteableBitmap.
Important : The WriteableBitmap is created in the main thread else the image object does'nt accept it.
Main thread create the WriteableBitmap:
_cameraWritableBitmap = new WriteableBitmap(
width,
height,
dpiX,
dpiY,
PixelFormats.Bgra32,
null);
_mainWindow.VideoWidget.Source = _cameraWritableBitmap;
The second thread process the images :
//I am in a thread (not the main thread)
Image<Gray, byte> img = GetFrame()
_cameraWritableBitmap.Lock();
//I edit the image and copy the image into _cameraWritableBitmap
...
//Display the image
_mainWindow.Dispatcher.BeginInvoke(new Action(() =>
{
_cameraWritableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
_cameraWritableBitmap.Unlock();
}), System.Windows.Threading.DispatcherPriority.Send);
Everything is fast except Dispatcher.BeginInvoke() which is a bit random and can take 20ms. I understand that the main thread has to be ready to execute my code but I wish to improve this part. unfortunately AddDirtyRect must be called in the main thread.
Is there a better way to display the images/video ? Maybe a method which allows the update directly from the thread ?
Thank you,