如何从 bmp 文件字节数组创建位图图像

本文关键字:数组 创建 位图 图像 字节数 字节 bmp 文件 | 更新日期: 2023-09-27 18:36:46

在我的wpf应用程序中,我得到了一个bmp文件的字节数组。

我想创建一个新的System.Windows.Media.Imaging.BitmapImage。

我从字节数组创建了MemoryStream,但它不适用于SetSource。

有什么建议吗?

如何从 bmp 文件字节数组创建位图图像

添加引用:

using System.IO;

使用以下代码。

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

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