WPF: How do I prevent tearing with WriteableBitmap?

帅比萌擦擦* 提交于 2019-12-20 10:43:25

问题


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

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.

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

MSDN documentation:
The UI thread writes content to the back buffer. The render thread reads content from the front buffer and copies it to video memory. Changes to the back buffer are tracked with changed rectangular regions.

< snip / >

When updates are sent to the rendering thread, the rendering thread copies the changed rectangles from the back buffer to the front buffer. The rendering system controls this exchange to avoid deadlocks and redraw artifacts, such as "tearing".

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.

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?


回答1:


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.




回答2:


While you are calling WritePixels() you may be overwriting your bitmap. Using Dispatcher.Invoke() rather then BeginInvoke() might help.




回答3:


I know this is an old thread but we had exactly the same issue and in our case it was caused by calling our update display method using Dispatcher.BeginInvoke - the moment we changed to Dispatcher.Invoke it cleared up instantly.



来源:https://stackoverflow.com/questions/1049446/wpf-how-do-i-prevent-tearing-with-writeablebitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!