我有一个 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
中。
关于c# - WPF BitmapFrame 和多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4940447/