Where to find the status of the WPF Image Control?

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!

Answers


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!


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...


Set an OnPropertyChanged on the Image DP?


Need Your Help

How to add camera damping?

c++ spring camera ogre

I asked a question about how to add camera damping in Ogre but didnt get any answer so here is a more vague question.

Timer in application_start to switch 'current deal'

c# asp.net asp.net-mvc-3 razor

Been trying to figure out the best method to accomplish this task and I will attempt to explain what I'm thinking as straight forward as possible.