简体   繁体   English

如何从bmp文件字节数组创建BitmapImage?

[英]How to create BitmapImage from a bmp file byte array?

In my wpf application I get a byte array of a bmp file. 在我的wpf应用程序中,我得到了bmp文件的字节数组。

I want to create a new System.Windows.Media.Imaging.BitmapImage. 我想创建一个新的System.Windows.Media.Imaging.BitmapImage。

I created MemoryStream from the byte array, but it doesn't work with SetSource. 我从字节数组创建了MemoryStream,但是它不适用于SetSource。

Any suggestions ? 有什么建议么 ?

Add reference: 添加参考:

using System.IO;

Use the following code. 使用以下代码。

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

For WPF 对于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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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