Reputation: 37
I'm loading BitmapImage from a url like this;
Uri uri = new Uri(item.url, UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
Image.Source = imgSource;
The code works fine but sometimes takes to much time to load the image, there is a way to detect that the image was loaded to enable a progress control while is loading?
Upvotes: 0
Views: 196
Reputation: 128061
Remarks and example from the BitmapImage.DownloadProgress event page on MSDN:
For cases where the async loading and decoding of a BitmapImage object are long enough to be noticeable to the user, an app can handle DownloadProgress on the source and display a ProgressRing or ProgressBar to indicate the progress state. These might display in the UI region that the image eventually displays in, or somewhere else in the UI. Use DownloadProgressEventArgs.Progress to modify the UI for a ProgressBar.
// somewhere in initialization
bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bi_DownloadProgress);
bitmapImage.ImageOpened += new EventHandler<ExceptionRoutedEventArgs>(bi_ImageOpened);
...
//progressBar is an existing control defined in XAML or extracted from a XAML template
void bi_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
progressBar.Value = e.Progress;
}
void bi_ImageOpened(object sender, RoutedEventArgs e)
{
progressBar.Visibility = Visibility.Collapsed;
}
Upvotes: 1