I’ve been using the BitmapImage.DownloadProgress event to wait until the image is loaded when creating a bitmap from a URI. That was I can use an event handler to check the Image.ActualWidth/ActualHeight after the download is complete. This works fine.
But when I am loading an image that the user has selected from their computer, the DownloadProgress is useless.
This works for URI bitmaps:
private void LoadImage(Uri uri) { this.ResetProgressImage(); BitmapImage image = new BitmapImage(); image.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(LoadImageDownloadProgress); image.UriSource = uri; this.ImageFull.Source = image; } private void LoadImageDownloadProgress(object sender, DownloadProgressEventArgs e) { this.Dispatcher.BeginInvoke(delegate { this.ProgressImage.Value = e.Progress; if (e.Progress == 100) { ImageHelper.Current.Width = Math.Round(this.ImageFull.ActualWidth); ImageHelper.Current.Height = Math.Round(this.ImageFull.ActualHeight); } }); }
But this doesn’t work when getting a BitmapImage from a stream:
private void LoadImage(Stream stream) { this.ResetProgressImage(); BitmapImage image = new BitmapImage(); image.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(LoadImageDownloadProgress); image.SetSource(stream); this.ImageFull.Source = image; }
Does anyone know an alternative to DownloadProgress, when using SetSource(stream)?
There is no event in BitmapImage to monitor the loading progress using a stream.
However, if you just need the image dimensions, you can add the image to the hierarchy and wait for the ‘Loaded’ or ‘LayoutUpdated’ event.