从非托管缓冲区创建图像

发布于 2025-01-23 17:49:56 字数 546 浏览 3 评论 0原文

我需要在C#编写的WPF应用程序中显示图像。图像内容由外部非托管库生成,该库将光栅缓冲区本身分配。

我正在尝试将该缓冲区包裹在bitmapsource对象(用image> Image> Control的source Control)使用创建方法,但是该方法采用一个字节数组,而我所拥有的只是intptr到缓冲区。

有没有办法从非托管缓冲区创建字节数组?最好不复制? (我知道我正在做的事情不安全,但是缓冲区可以在应用程序的整个生命周期内持续存在)。

还是有另一种方法可以将栅格图像从未托管的缓冲区显示到imagecanvas对象或类似方法中?


更新:

我只是错过了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2025-01-30 17:49:56

为了从非管理的RAW PIXEL缓冲区创建BitMapsource,请使用 bitmapsource.create.create 方法,例如这样:

PixelFormat format = PixelFormats.Bgr24;
int width = 768;
int height = 576;
int stride = (width * format.BitsPerPixel + 7) / 8;
int size = stride * height;

IntPtr buffer = ...

BitmapSource bitmap = BitmapSource.Create(
    width, height, 96, 96, format, null, buffer, size, stride);

如果您想周期性地更新图像元素的来源,可能更有效地覆盖单个可文字标记的缓冲区,而不是在每个周期中使用新的BitMapsource重新分配图像的源。

In order to create a BitmapSource from an unmanaged raw pixel buffer, use the BitmapSource.Create method, e.g. like this:

PixelFormat format = PixelFormats.Bgr24;
int width = 768;
int height = 576;
int stride = (width * format.BitsPerPixel + 7) / 8;
int size = stride * height;

IntPtr buffer = ...

BitmapSource bitmap = BitmapSource.Create(
    width, height, 96, 96, format, null, buffer, size, stride);

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.

初相遇 2025-01-30 17:49:56

我建议使用 WritableBitMap 。尽管这确实需要副本,但它不需要任何分配,我希望性能在实时视图的要求之内。

myWriteableBitmap.WritePixels(
    new Int32Rect(0, 0, width, height),
    bufferPointer,
    bufferSize,
    stride);

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.

myWriteableBitmap.WritePixels(
    new Int32Rect(0, 0, width, height),
    bufferPointer,
    bufferSize,
    stride);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文