我在Blob存储中的云中存储了一个PNG文件,我想下载该文件并在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;
}
如果我尝试冻结Frame [0],我会收到一个异常消息,说该Frame无法冻结。 BitmapDecoder.Create
返回的解码器也是[[not一个PngBitmapDecoder
,但是一个LateBoundBitmapDecoder
,我真的不知道如何有效地使用它。
WriteableBitmap
中。 WriteableBitmap
中是可以的,但是该类型具有前后缓冲区,这使内存占用量增加了一倍。 CachedBitmap
是更好的选择。new CachedBitmap(bitmapSource, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
除非感到意外,否则请使用BitmapCacheOption.OnLoad
,并记住也要冻结结果对象。