WPF BitmapFrame 和多线程

Mar*_*ark 3 c# wpf multithreading bitmap

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

我知道调度程序和冻结,但没有任何作用。我不断收到有关“另一个线程拥有它”的错误。

这是我所拥有的:

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;
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试冻结 Frame[0],我会收到一个异常,提示该 Frame 无法冻结。另外,返回的解码器BitmapDecoder.Create不是a而是 a我真的知道如何有效使用。PngBitmapDecoderLateBoundBitmapDecoder

boh*_*nko 5

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

故事很长,有代码。