WPF BitmapFrame和多个线程 [英] WPF BitmapFrame and multiple threads

查看:77
本文介绍了WPF BitmapFrame和多个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Blob存储中的云中存储了一个PNG文件,我想下载该文件并将其呈现在WPF中的屏幕上.

I have a PNG file stored in my cloud in blob storage, I want to download it and render it on the screen in WPF.

我了解分派器和冻结,但是没有任何效果.我不断收到有关另一个线程拥有它"的错误.

I know about the Dispatcher and Freezing, but nothing is working. I keep getting the error about "another thread owns it".

这就是我所拥有的:

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 返回的Decoder不是 PngBitmapDecoder ,而是 LateBoundBitmapDecoder ,我不知道该怎么做.有效使用.

If i try to freeze the Frame[0] I get an exception saying that this Frame cannot be frozen. Also the Decoder returned by BitmapDecoder.Create is not a PngBitmapDecoder but a LateBoundBitmapDecoder which I dont really know how to use effectively.

推荐答案

是否可能不仅需要在调度程序上创建Bitmapframe,而且还需要BitmapDecoder?您是否尝试过在调度程序上调用GetDecoder?

Its possible that you need to not only create the Bitmapframe on the dispatcher but also the BitmapDecoder? Have you tried invoking the GetDecoder on the dispatcher?

这篇关于WPF BitmapFrame和多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆