c# - WPF BitmapFrame 和多线程

标签 c# wpf multithreading bitmap

我有一个 PNG 文件存储在我的云中的 blob 存储中,我想下载它并在 WPF 屏幕上呈现它。

我知道 Dispatcher 和 Freezing,但没有任何效果。我不断收到关于“另一个线程拥有它”的错误。

这是我的:

var decoder = GetDecoder("http://address/image.png");

Dispatcher.Invoke(DispatcherPriority.Send, new Action<BitmapFrame>(SetImage), decoder.Frames[0]);

public void SetImage(BitmapFrame source)
{
    var bitmapFrame = BitmapFrame.Create(source);  //ERROR HERE!!!!!!!!
    LazyImage.Source = bitmapFrame;
}

private BitmapDecoder GetDecoder(object uri)
{
    var extension = System.IO.Path.GetExtension((string)uri);
    BitmapDecoder decoder = null;
    if (extension.ToLower() == ".png")
        decoder = BitmapDecoder.Create(new Uri((string)uri, UriKind.Absolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
    return decoder;
}

如果我尝试卡住 Frame[0],我会收到一个异常,提示无法卡住该 Frame。此外,BitmapDecoder.Create 返回的解码器不是 PngBitmapDecoder 而是一个LateBoundBitmapDecoder,我真的不知道该怎么做有效使用。

最佳答案

简而言之:尝试将结果包装到 WriteableBitmap 中。

Long story, with code.

关于c# - WPF BitmapFrame 和多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4940447/

相关文章:

c# - 如何在 .NET Core 2.1 中使用 String.Create 的示例

c# - WPF多级绑定(bind)线程问题

java - 线程连接自身

c# - 如何在 app.config 中创建自定义标签

c# - 浓缩 if 语句错误

c# - 如何等到 MSTSC.exe 退出

c# - 将 ViewModel 添加为 app.xaml 中的资源是一种好习惯吗?

wpf - 如何使 WPF 数据模板填充列表框的整个宽度?

c++ - setjmp 和 longjump 实现线程

c# - 传递给其他线程的对象上的锁会怎样?