获取 wpf WriteableBitmap 的 DrawingContext
有没有办法获取 WriteableBitmap
的 DrawingContext
(或类似的东西)? 即允许您调用简单的 DrawLine
/DrawRectangle
/etc 类型的方法,而不是直接操作原始像素。
Is there a way to get a DrawingContext
(or something similar) for a WriteableBitmap
? I.e. something to allow you to call simple DrawLine
/DrawRectangle
/etc kinds of methods, rather than manipulate the raw pixels directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现 Sixlettervariables 的解决方案是最可行的。 但是,缺少“drawingContext.Close()”。 根据 MSDN 的说法,“必须先关闭 DrawingContext,然后才能呈现其内容”。
结果是以下实用函数:
然后可以像这样轻松地使用它:
I found sixlettervariables' solution the most workable one. However, there's a "drawingContext.Close()" missing. According to MSDN, "A DrawingContext must be closed before its content can be rendered".
The result is the following utility function:
This can then easily be used like this:
如果您不介意使用
System.Drawing
你可以这样做:If you don't mind using
System.Drawing
you could do something like:我想知道同样的事情,因为目前我正在做类似的事情:
我正在尝试使用 WriteableBitmap 来允许多线程访问像素缓冲区,目前 DrawingContext 和 RenderTargetBitmap 都不允许这样做。 也许某种基于您从 RenderTargetBitmap 检索到的内容的 WritePixels 例程会起作用?
I'm wondering the same thing, as currently I do something like:
I'm trying to use the WriteableBitmap to allow multithreaded access to the pixel buffer, which is currently not allowed with neither a DrawingContext nor a RenderTargetBitmap. Maybe some sort of WritePixels routine based off of what you've retrieved from the RenderTargetBitmap would work?
看来这个词是不。
为了将来的参考,我们计划使用 WPF 的 可写位图扩展 的端口。
对于使用纯粹现有代码的解决方案,下面提到的任何其他建议都可以。
It appears the word is no.
For future reference, we plan to use a port of the Writeable Bitmap Extensions for WPF.
For a solution using purely existing code, any of the other suggestions mentioned below will work.
解决此问题的另一种方法是使用 RenderTargetBitmap 作为后备存储,就像在 WriteableBitmap 示例中一样。 然后,您可以随时创建并向其发出 WPF 绘图命令。 例如:
如果您想每帧重绘此
RenderTargetBitmap
,您可以捕获CompositionTarget.Rendering
事件,如下所示:A different way to solve this problem is to use a
RenderTargetBitmap
as a backing store, just like in theWriteableBitmap
example. Then you can create and issue WPF drawing commands to it whenever you want. For example:If you want to redraw this
RenderTargetBitmap
every frame, you can catch theCompositionTarget.Rendering
event, like this: