How can I convert byte[] to BitmapImage?

后端 未结 4 795
死守一世寂寞
死守一世寂寞 2021-01-17 12:03

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

4条回答
  •  野的像风
    2021-01-17 12:09

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

提交回复
热议问题