WPF: Best way to render WriteableBitmap if bitmap is updated frequently
I am currently implementing a wideband sonar display using WriteableBitmap in WPF. The waterfall broadband sonar display starts at the top and over time the history data moves downward, with the first โlineโ always showing the current situation.
Technically, I first move the content of the Bitmap using WriteableBitmap.CopyPixels (), after which I update the array to update the current row (top) of the Drive.
My problem is - while updating the bitmap - the screen flickers. I tried to write my own WritePixels implementation:
public static unsafe void WritePixels(WriteableBitmap writeableBitmap, BitmapProperties bitmapProperties)
{
writeableBitmap.Lock();
IntPtr buff = writeableBitmap.BackBuffer;
byte* pbuff = (byte*)buff.ToPointer();
for (int i = 0; i < bitmapProperties.BitmapArray.Length; i += bitmapProperties.BytesPerPixel)
{
pbuff[i] = bitmapProperties.BitmapArray[i];
pbuff[i + 1] = bitmapProperties.BitmapArray[i + 1];
pbuff[i + 2] = bitmapProperties.BitmapArray[i + 2];
pbuff[i + 3] = bitmapProperties.BitmapArray[i + 3];
}
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, (int)writeableBitmap.Width, (int)writeableBitmap.Height));
writeableBitmap.Unlock();
}
Unfortunately, the ouctome is the same.
I've seen some similar questions here (medical ultrasound display implementation), but it's a little different here, since I'm not getting an image from a third party C ++ interface, but I'm drawing and copying the bitmap myself (modifying / copying the bitmap). Decorating (updating the bitmap) should be done every ~ 250 milliseconds.
What would be my best pick here ... working with a cached bitmap? I have little experience when it comes to low-level bitmap operations in WPF (I'm using WPF 4.5).
Thank.
EDIT: I compared it to a reference application written in C ++ / DirectX: To my surprise, I can see "flickering" even there. There may be some optical effects that disturb the eyes, etc. However, this question still matters if there is a better approach than my current implementation.
source to share
Have you tried to measure if there is enough time to update the entire image? Maybe double buffering can help you:
- Copy all bitmaps
- Add your changes or replace the copy of the image in the background thread.
- If the job is done, switch the prepared image from the background thread with the currently visible image
- Make changes again to the image that is not currently visible and so on ...
source to share