views:

1516

answers:

2

Hi, there is my first time on Stack Overflow, thanks in advance for any help I get.

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)?

+2  A: 

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.

BC
A: 

Thanks BC,

That was a good idea. It does work, but you have to remember to remove the EventHandler after it's been used, otherwise my event was triggering off for other layout changes.

While I was waiting for an answer, by solution was to create a seperate Thread and Thread.Sleep(200) and then call my method, this worked also, but seemed a bit like a hack.

Thanks for your help!

Findel

Findel_Netring