WPF BitmapFrame和多个线程

问题描述 投票:1回答:3

我在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,我真的不知道如何有效地使用它。

c# wpf multithreading bitmap
3个回答
1
投票
可能不仅需要在调度程序上创建Bitmapframe,而且还需要BitmapDecoder?您是否尝试在调度程序上调用GetDecoder?

2
投票
简而言之:尝试将结果包装到WriteableBitmap中。

Long story, with code.


0
投票
今天仍然是问题!按照Bohdan的建议,将数据包装在WriteableBitmap中是可以的,但是该类型具有前后缓冲区,这使内存占用量增加了一倍。 CachedBitmap是更好的选择。

new CachedBitmap(bitmapSource, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

除非感到意外,否则请使用BitmapCacheOption.OnLoad,并记住也要冻结结果对象。    
© www.soinside.com 2019 - 2024. All rights reserved.