2012-11-16 31 views
2

我用这个代码从互联网获取的图像的BitmapImage - 图像下载问题

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; 

有时没有图片... 看来,这是因为超时等..

无论如何,我使用一些异步。及时获取图像的方法?

任何线索?

谢谢!

回答

3

导入图像异步(C#5.0和.NET Framework 4.5):

var bytes = await new WebClient().DownloadDataTaskAsync(url); 

var image = new BitmapImage(); 
image.BeginInit(); 
image.CacheOption = BitmapCacheOption.OnLoad; 
image.StreamSource = new MemoryStream(bytes); 
image.EndInit(); 
RSSImage.Source = image; 
+0

只是不要忘了'WebClient'使用'using'语句中 –