image

Sascha Depold

Engineering Manager

Blog

Loading an image asynchronously in C#

Loading an image asynchronously in C#

I just needed a way to load images asynchronously in a Windows Phone 7 application. This article explains the very nice way to go. I just shortened the presented code a little bit:

private void updateImage(Image i, Uri uri)
{
WebClient client = new WebClient();

client.OpenReadCompleted += new OpenReadCompletedEventHandler(delegate(object sender, OpenReadCompletedEventArgs e) {
BitmapImage imageToLoad = new BitmapImage();
imageToLoad.SetSource(e.Result as Stream);
i.Source = imageToLoad;
});

client.OpenReadAsync(uri, uri.AbsoluteUri);
}

The method awaits an Image and an Uri object. Have fun :)