您应该异步下载 Web 图像并从下载的缓冲区创建 BitmapFrame:
public async Task<BitmapFrame> GetBitmapFrame(Uri uri)
{
var httpClient = new System.Net.Http.HttpClient();
var buffer = await httpClient.GetByteArrayAsync(uri);
using (var stream = new MemoryStream(buffer))
{
return BitmapFrame.Create(
stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
由于BitmapFrame.Create
上述示例中的调用返回一个冻结的 BitmapFrame,因此您也可以异步创建 BitmapFrame(尽管我怀疑这是必要的)。
public async Task<BitmapFrame> GetBitmapFrame(Uri uri)
{
var httpClient = new System.Net.Http.HttpClient();
var buffer = await httpClient.GetByteArrayAsync(uri);
return await Task.Run(() =>
{
using (var stream = new MemoryStream(buffer))
{
return BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
});
}