How can I convert byte[] to BitmapImage?

纵然是瞬间 提交于 2019-12-12 10:31:02

问题


I have a byte[] that represents the raw data of an image. I would like to convert it to a 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.


回答1:


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.

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

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);



回答2:


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;
    }
}



回答3:


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!

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

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 OracleBlob object. Instead of filling a dataset/datatable, I did this:

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 myArray and do this:

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

And my revised LoadBitmapFromBytes function from the other answer:

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;
}



回答4:


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

Like this:

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


来源:https://stackoverflow.com/questions/15270844/how-can-i-convert-byte-to-bitmapimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!