NoWar
NoWar

Reputation: 37633

BitmapImage - image downloading issue

I use this code to get images from Internet

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;

And sometimes there are no images.

It seems that it happens because of the timeout and etc.

Anyway have I use some async. approaches to get image in time?

Any clue?

Upvotes: 2

Views: 3216

Answers (1)

skink
skink

Reputation: 5736

Loading image asynchronously (C# 5.0 and .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;
}

Upvotes: 4

Related Questions