如何将WPF图像的源设置为C#代码后面的bytearray? [英] How do I set a WPF Image's Source to a bytearray in C# code behind?

查看:168
本文介绍了如何将WPF图像的源设置为C#代码后面的bytearray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#/ WPF构建一个小应用程序。

I'm building a small app using C#/WPF.

应用程序(从非托管C ++库)接收来自a的字节数组(byte [])位图源

The application receives (from an unmanaged C++ library) a byte array (byte[]) from a bitmap source

在我的WPF窗口中,我有一个(System.windows.Controls.Image)图像,我将用它来显示位图。

In my WPF window, I have an (System.windows.Controls.Image) image which I will use to display the bitmap.

在后面的代码(C#)中,我需要能够获取该字节数组,创建BitmapSource / ImageSource并为我的图像控件分配源。

In the code behind (C#) I need to able to take that byte array, create BitmapSource /ImageSource and assign the source for my image control.

// byte array source from unmanaged librariy
byte[] imageData; 

// Image Control Definition
System.Windows.Controls.Image image = new Image() {width = 100, height = 100 };

// Assign the Image Source
image.Source = ConvertByteArrayToImageSource(imageData);

private BitmapSource ConvertByteArrayToImagesource(byte[] imageData)
{
    ??????????
}

我在这里工作了一段时间而且还没有能够弄清楚这一点。我已经尝试过几种解决方案,这些解决方案都是我通过各种方式找到的。到目前为止,我还没弄清楚这一点。

I've been working on this for a bit here and haven't been able to figure this out. I've tried several solutions that I've found by goolging around. To date, I haven't been able to figure this out.

我试过:

1)创建BitmapSource

var stride = ((width * PixelFormats.Bgr24 +31) ?32) *4);
var imageSrc = BitmapSource.Create(width, height, 96d, 96d, PixelFormats.Bgr24, null, imageData, stride);

通过运行时异常说缓冲区太小
缓冲区大小不够

That through a runtime exception saying buffer was too small Buffer size is not sufficient

2)我尝试使用内存流:

BitmapImage bitmapImage = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
   bitmapImage.BeginInit();
   bitmapImage.CrateOptions = BitmapCreateOptions.PreservePixelFormat;
   bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
   bitmapImage.StreamSource = mem;
   bitmapImage.EndInit();
   return bitmapImage;
}

此代码通过EndInit()调用的异常。
找不到适合完成此操作的成像组件。

This code through an exception on the EndInit() call. "No imaging component suitableto complete this operation was found."

SOS!我在这个上花了几天时间,显然卡住了。
任何帮助/想法/方向将不胜感激。

SOS! I've spent a couple of days on this one and am clearly stuck. Any help/ideas/direction would be greatly appreciated.

谢谢,
JohnB

Thanks, JohnB

推荐答案

你的步幅计算是错误的。它是每个扫描行的完整字节数,因此应按如下方式计算:

Your stride calculation is wrong. It is the number of full bytes per scan line, and should therefore be calculated like this:

var format = PixelFormats.Bgr24;
var stride = (width * format.BitsPerPixel + 7) / 8;

var imageSrc = BitmapSource.Create(
    width, height, 96d, 96d, format, null, imageData, stride);

当然你还必须确保使用正确的图像尺寸,即 width height 值实际上与 imageBuffer 中的数据相对应。

Of course you also have to make sure that you use the correct image size, i.e. that the width and height values actually correspond with the data in imageBuffer.

这篇关于如何将WPF图像的源设置为C#代码后面的bytearray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆