简体   繁体   中英

How to create BitmapImage from a bmp file byte array?

In my wpf application I get a byte array of a bmp file.

I want to create a new System.Windows.Media.Imaging.BitmapImage.

I created MemoryStream from the byte array, but it doesn't work with SetSource.

Any suggestions ?

Add reference:

using System.IO;

Use the following code.

MemoryStream ms = new MemoryStream(imageArray);
Image image = Image.FromStream(ms);

For WPF

public static BitmapImage GetBitmapImage(byte[] imageArray)
{
    using (var stream = new MemoryStream(imageArray))
    {
        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = stream;
        bitmapImage.EndInit();
        return bitmapImage;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM