wpf 如何将 byte[] 转换为 BitmapImage?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15270844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:56:10  来源:igfitidea点击:

How can I convert byte[] to BitmapImage?

wpfimagetype-conversionbitmapimage

提问by gerstla

I have a byte[]that represents the raw data of an image. I would like to convert it to a BitmapImage.

我有一个byte[]代表图像的原始数据。我想将其转换为BitmapImage.

I tried several examples I found but I kept getting the following exception

我尝试了几个我发现的例子,但我一直收到以下异常

"No imaging component suitable to complete this operation was found."

“没有找到适合完成此操作的成像组件。”

I think it is because my byte[]does not actually represent an Image but only the raw bits. so my question is as mentioned above is how to convert a byte[] of raw bits to a BitmapImage.

我认为这是因为我byte[]实际上并不代表图像,而只是代表原始位。所以我的问题如上所述是如何将原始位的字节 [] 转换为BitmapImage.

采纳答案by Clemens

When your byte array contains a bitmap's raw pixel data, you may create a BitmapSource(which is the base class of BitmapImage) by the static method BitmapSource.Create.

当您的字节数组包含位图的原始像素数据时,您可以通过静态方法创建一个BitmapSource(它是 的基类BitmapImageBitmapSource.Create

However, you need to specify a few parameters of the bitmap. You must know in advance the width and height and also the PixelFormatof the buffer.

但是,您需要指定位图的一些参数。您必须事先知道PixelFormat缓冲区的宽度和高度以及缓冲区的宽度和高度。

byte[] buffer = ...;

var width = 100; // for example
var height = 100; // for example
var dpiX = 96d;
var dpiY = 96d;
var pixelFormat = PixelFormats.Pbgra32; // for example
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
var stride = bytesPerPixel * width;

var bitmap = BitmapSource.Create(width, height, dpiX, dpiY,
                                 pixelFormat, null, buffer, stride);

回答by Eirik

public static BitmapImage LoadFromBytes(byte[] bytes)
{
    using (var stream = new MemoryStream(bytes))
    {
        stream.Seek(0, SeekOrigin.Begin);
        var image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();

        return image;
    }
}

回答by vapcguy

I ran across this same error, but it was because my array was not getting filled with the actual data. I had an array of bytes that was equal to the length it was supposed to be, but the values were all still 0- they had not been written to!

我遇到了同样的错误,但这是因为我的数组没有填充实际数据。我有一个字节数组,它的长度等于它应该是的长度,但这些值都是静止的0——它们没有被写入!

My particular issue - and I suspect for others that arrive at this question, as well - was because of the OracleBlobparameter. I didn't think I needed it, and thought I could just do something like:

我的特殊问题 - 我怀疑其他人也遇到了这个问题 - 是因为OracleBlob参数。我不认为我需要它,并认为我可以这样做:

DataSet ds = new DataSet();
OracleCommand cmd = new OracleCommand(strQuery, conn);
OracleDataAdapter oraAdpt = new OracleDataAdapter(cmd);
oraAdpt.Fill(ds)

if (ds.Tables[0].Rows.Count > 0)
{
    byte[] myArray = (bytes)ds.Tables[0]["MY_BLOB_COLUMN"];
}

How wrong I was! To actually get the real bytes in that blob, I needed to actually read that result into an OracleBlobobject. Instead of filling a dataset/datatable, I did this:

我错了!为了实际获得该 blob 中的实际字节,我需要将该结果实际读入一个OracleBlob对象中。我没有填充数据集/数据表,而是这样做了:

OracleBlob oBlob = null;
byte[] myArray = null;
OracleCommand cmd = new OracleCommand(strQuery, conn);
OracleDataReader result = cmd.ExecuteReader();
result.Read();

if (result.HasRows)
{
    oBlob = result.GetOracleBlob(0);
    myArray = new byte[oBlob.Length];
    oBlob.Read(array, 0, Convert.ToInt32(myArray.Length));
    oBlob.Erase();
    oBlob.Close();
    oBlob.Dispose();
}

Then, I could take myArrayand do this:

然后,我可以采取myArray并做到这一点:

if (myArray != null)
{
   if (myArray.Length > 0)
   {
       MyImage.Source = LoadBitmapFromBytes(myArray);
   }
}

And my revised LoadBitmapFromBytesfunction from the other answer:

LoadBitmapFromBytes在另一个答案中修改过的函数:

public static BitmapImage LoadBitmapFromBytes(byte[] bytes)
{
    var image = new BitmapImage();
    using (var stream = new MemoryStream(bytes))
    {
        stream.Seek(0, SeekOrigin.Begin);
        image.BeginInit();
        image.StreamSource = stream;
        image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = null;
        image.EndInit();
    }

    return image;
}

回答by crazylpfan

Create a MemoryStream from the raw bytes and pass that into your Bitmap constructor.

从原始字节创建一个 MemoryStream 并将其传递到您的 Bitmap 构造函数中。

Like this:

像这样:

MemoryStream stream = new     MemoryStream(bytes);
Bitmap image = new Bitmap(stream);