3

我目前正在通过在 WPF 中使用 WriteableBitmap 来实现宽带声纳显示。宽带声纳“瀑布”显示从顶部开始,随着时间的推移,历史数据向下移动,第一“行”始终显示当前情况。

从技术上讲,我首先使用 WriteableBitmap.CopyPixels() 将位图的内容向下移动,然后更新数组以更新显示的当前行(顶部)。

我现在的问题是 - 在位图更新期间 - 屏幕闪烁。我尝试编写自己的 WritePixels 实现:

 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();

    }

不幸的是,ouctome 是一样的。

我在这里看到了一些类似的问题(实现医疗超声显示),但这里的用例略有不同,因为我不是从第三方 c++ 界面获取图片,而是我自己“绘制”并复制位图(更改/复制位图数组)。渲染(位图的更新)应每 ~ 250 毫秒发生一次。

我在这里最好的选择是什么……使用缓存的位图?对于 WPF 中的低级位图操作(我使用的是 WPF 4.5),我没有太多经验。

谢谢。

编辑:我已经将它与一个用 C++/DirectX 编写的参考应用程序进行了比较:令我惊讶的是,我看到即使发生了“闪烁”。可能是一些光学效应,干扰眼睛等。然而,关于是否有比我目前的实现更好的方法,这个问题仍然有效。

4

2 回答 2

0

您是否尝试过衡量刷新整个图像是否需要太长时间?也许双缓冲可以帮助你:

  • 复制整个位图
  • 添加您的更改或在后台线程中替换图像的副本
  • 如果工作完成,从后台线程切换准备好的图像与当前可见的图像
  • 再次对当前不可见的图像进行更改等等......
于 2013-03-17T16:23:11.697 回答
0

也许你可以在 kernel32.dll 中使用 CopyMemory,这样更快

[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

private Data2Image(int width, int height, IntPtr data, WriteableBitmap)
{
    writeableBitmap.Lock();            
    try
    {
        CopyMemory(writeableBitmap.BackBuffer, data, (uint)(width * height * 4));
        writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
    }
    finally
    {
        writeableBitmap.Unlock();
    }
}
于 2020-01-06T10:02:24.793 回答