我有一个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
我真的不知道如何有效使用。
答案 0 :(得分:2)
简而言之:尝试将结果包装到WriteableBitmap
。
答案 1 :(得分:1)
您可能不仅需要在调度程序上创建Bitmapframe,还需要在BitmapDecoder中创建Bitmapframe吗?您是否尝试在调度程序上调用GetDecoder?
答案 2 :(得分:0)
今天仍然是一个问题!按照Bohdan的建议,将数据包装在WriteableBitmap
中是可行的,但是该类型具有一个前后缓冲区,从而使内存占用量增加了一倍。 CachedBitmap
是更好的选择。
new CachedBitmap(bitmapSource, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
除非有惊喜,否则请使用BitmapCacheOption.OnLoad
,并记住也要冻结结果对象。