WPF:如何prevent与WriteableBitmap的撕裂?WPF、prevent、WriteableBitmap

2023-09-07 03:14:59 作者:人生若只如初见

我使用的是WriteableBitmap以每秒约20帧,以显示我处理自己的图像。

I'm using a WriteableBitmap to display images I process myself at around 20 frames per second.

此问题(WPF: 显示快速变化的图像?)的更有效的方法 而这个问题(How没有大的内存分配,以显示快速更新图像?) 表明,要做到这一点的最好办法是使用 WriteableBitmap的

This question (WPF: More efficient way of displaying quickly-changing images?) and this question (How to display quick-updating images without large memory allocation?) indicate that the best way to do this is by using a WriteableBitmap.

的文档 WriteableBitmap的表示调用 WritePixels() UI线程会导致渲染线程重绘上图片:

The documentation for WriteableBitmap indicates that calling WritePixels() on the UI thread will cause the rendering thread to redraw the image:

MSDN文档: 的UI线程写入内容到后台缓冲区。渲染线程读取来自前台缓冲区并将其复制到视频内存的内容。更改到后台缓冲区跟踪与改变的矩形区域。的

<喀嚓/> 的

当更新发送到渲染线程,渲染线程拷贝改变的矩形从后台缓冲区到前台缓冲区。渲染系统控制此交换,以避免死锁和重绘文物,如撕裂。的

我处理在后台线程我的图片,然后使用 Dispatcher.BeginInvoke()来调用 WritePixels() ,以确保 WritePixels()被称为UI线程。

I process my images on a background thread, then use Dispatcher.BeginInvoke() to call WritePixels(), to ensure that WritePixels() is called on the UI thread.

我发现撕裂仍然出现 WriteableBitmap的,并在应用我的工作,它看起来可怕的(其医疗成像应用程序)。有什么我可以做什么?

I'm finding that tearing still occurs with WriteableBitmap, and in the application I'm working on, it looks awful (its a medical imaging application). Is there anything I can do?

推荐答案

有大量的工作投入WriteableBitmap的,以避免撕裂,但在某些系统配置,这是不可避免的。这主要将发生在Windows XP或Vista瓦特/航空(DWM)关闭。

There was a lot of work put into WriteableBitmap to avoid tearing, but on some system configurations this is unavoidable. This mostly will happen on Windows XP or Vista w/ Aero (DWM) turned off.