【问题标题】:'BitmapImage' does not contain a definition for 'SetSource' and no accessible extension method 'SetSource' [duplicate]“BitmapImage”不包含“SetSource”的定义,也没有可访问的扩展方法“SetSource”[重复]
【发布时间】:2022-02-03 01:58:41
【问题描述】:

我正在尝试使用转换器中的方法 SetSource 将字节数组转换为 BitmapImage,但不幸的是,该方法看起来没有了。

我检查了文档,该方法仍在使用中。 链接:https://docs.microsoft.com/en-us/previous-versions/cc189859(v=vs.95)

有什么想法吗?

public static BitmapImage ConvertByteArrayToBitMapImage(byte[] imageByteArray)
    {
        BitmapImage img = new BitmapImage();
        using (MemoryStream memStream = new MemoryStream(imageByteArray))
        {
            img.SetSource(memStream);
        }
        return img;
    }

【问题讨论】:

标签: c# wpf


【解决方案1】:

我必须通知BitmapImage初始化的开始和结束,并且在EndInit之后关闭流时还要设置BitmapCacheOption.OnLoad

using (MemoryStream memStream = new MemoryStream(imageByteArray))
{ 
    img.BeginInit();
    img.StreamSource = memStream;
    img.CacheOption = BitmapCacheOption.OnLoad;
    img.EndInit();
}

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多