从非托管缓冲区创建图像
我需要在C#编写的WPF应用程序中显示图像。图像内容由外部非托管库生成,该库将光栅缓冲区本身分配。
我正在尝试将该缓冲区包裹在bitmapsource
对象(用image> Image> Control的
source
Control)使用创建方法,但是该方法采用一个字节数组,而我所拥有的只是
intptr
到缓冲区。
有没有办法从非托管缓冲区创建字节数组?最好不复制? (我知道我正在做的事情不安全,但是缓冲区可以在应用程序的整个生命周期内持续存在)。
还是有另一种方法可以将栅格图像从未托管的缓冲区显示到image
或canvas
对象或类似方法中?
更新:
我只是错过了bitmapsource.create
的过载,它采用intptr
! (尽管我不知道这是否会复制图像。)
I need to display an image in a WPF application written in C#. The image content is generated by an external unmanaged library, which allocates the raster buffer itself.
I am trying to wrap that buffer in a BitmapSource
object (attached as the Source
of an Image
control) using the Create
method, but the method takes a byte array whereas all I have is an IntPtr
to the buffer.
Is there a way to create a byte array from an unmanaged buffer ? Preferably without copying ? (I know that what I am doing is unsafe, but the buffer is guaranteed to persist for the whole lifetime of the application).
Or is there an alternative way to display a raster image from an unmanaged buffer into an Image
or Canvas
object or similar ?
Update:
I just missed that there is an overload of BitmapSource.Create
that takes an IntPtr
! (Though I don't know if that copies the image or not.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了从非管理的RAW PIXEL缓冲区创建BitMapsource,请使用 bitmapsource.create.create 方法,例如这样:
如果您想周期性地更新图像元素的来源,可能更有效地覆盖单个可文字标记的缓冲区,而不是在每个周期中使用新的BitMapsource重新分配图像的源。
In order to create a BitmapSource from an unmanaged raw pixel buffer, use the BitmapSource.Create method, e.g. like this:
In case you want to cyclically update the Source of an Image element, it may may be more efficient to overwrite the buffer of a single WriteableBitmap instead of reassigning the Image's Source with a new BitmapSource in each cycle.
我建议使用 WritableBitMap 。尽管这确实需要副本,但它不需要任何分配,我希望性能在实时视图的要求之内。
I would suggest using a WriteableBitmap. While this do require a copy, it should not need any allocations, and I would expect the performance to be well within the requirements for a live view.