error in my byte[] to WPF BitmapImage conversion?

∥☆過路亽.° 提交于 2019-11-29 11:52:28
gcores

Does this StackOverflow question helps?

byte[] to BitmapImage in silverlight

EDIT:

Try this, not sure it will work:

public BitmapImage convertByteToBitmapImage(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    stream.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.DecodePixelWidth = ??; // Width of the image
    bi.StreamSource = stream;
    bi.EndInit();
    return bi;
}

UPDATE 2:

I found these:

Load a byte[] into an Image at Runtime

BitmapImage from byte[] on a non UIThread

Apart from that, I don't know.

How do you know that the byte[] format you are creating is what the BI expects in the Stream? Why don't you use the BitmapImage.StreamSource to create the byte[] that you save? Then you know the format will be compatible.

http://www.codeproject.com/KB/vb/BmpImage2ByteArray.aspx

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8327dd31-2db1-4daa-a81c-aff60b63fee6/

[I did not try any of this code, but you can]

Turns out the bitmapimage CopyPixels isn't right. I take the output of the bitmapimage and convert it to something usable in this case a jpg.

public static Byte[] convertBitmapImageToBytestream(BitmapImage bi)
  {
     MemoryStream memStream = new MemoryStream();
     JpegBitmapEncoder encoder = new JpegBitmapEncoder();
     encoder.Frames.Add(BitmapFrame.Create(bi));
     encoder.Save(memStream);
     byte[] bytestream = memStream.GetBuffer();
     return bytestream;
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!