我使用此代码从互联网获取图像
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(url, UriKind.Absolute);
image.EndInit();
RSSImage.Source = image;
有时没有图像。
看来是因为超时等原因发生的。
无论如何,我使用了一些异步。如何及时获取图像?
有什么线索吗?
最佳答案
异步加载图像(C# 5.0 和 .NET Framework 4.5):
using (var client = new WebClient()) {
var bytes = await client.DownloadDataTaskAsync(url);
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = new MemoryStream(bytes);
image.EndInit();
RSSImage.Source = image;
}
关于c# - BitmapImage - 图像下载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13415471/