views:

483

answers:

3

Hello all,

One of the nice feature of the Image control is that we can specified an Uri as the ImageSource and the image is automatically downloaded for us. This is great! However, the control doesn't seem to have a property indicating if the image loading is in progress or not.

Is there a property telling us the status (Downloading, Downloaded, etc.) of the Image control?

Thanks!

A: 

Hmm - that's a good question. I looked at the ImageSource class's documentation on MSDN, and it doesn't look like there is anything on there to get that information.

That being said, could you start the download manually and set the Image control's Source property once that download finishes? You'd know for sure if the download was completed...

unforgiven3
I have no control over the download... I simply set the Uri and I am done. Of course I could try to reinvent the wheel and download myself the pictures, display them, etc... but I don't think I want to do that...
Martin
A: 

Set an OnPropertyChanged on the Image DP?

Paul Betts
+2  A: 

As long as your ImageSource is a BitmapImage you could use the BitmapImage.DownloadCompleted event. The only problem I have found so far is that it only works from C#, so you would lose some flexibility. I'm guessing you could access that event from XAML, but I'm not sure how. The following sample starts loading the image with the click of a button, and updates a label when the image finished loading.

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Image x:Name="image" Grid.Row="2"/>
    <Label x:Name="label" Content="aaa" Grid.Row="1"  />
    <Button Click="Button_Click" Content="Click to load image" Grid.Row="0" />
</Grid>

Code:

private void Button_Click(object sender, RoutedEventArgs e)
{
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.DecodePixelHeight = 100;
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.UriSource = new Uri("bigImageUri");
    bi.EndInit();

    bi.DownloadCompleted += new EventHandler(bi_DownloadCompleted);
    image.Source = bi; 


}

void bi_DownloadCompleted(object sender, EventArgs e)
{
    label.Content = "dl completed";
}

Hope it helps!

Ramiro Berrelleza