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!


Need Your Help

Differences in terminal prompt for different users on server

unix terminal user vps

I am build a Digital Ocean VPS for my site but got a weird little problem.

On iOS, is doing CGContextRelease required?

ios core-animation core-graphics automatic-ref-counting cgcontextref

Because if Instruments is run and Activities Monitor is chosen, the app running on iPhone 4S is using 4.88MB if the contexts are released, and is also 4.88MB if the contexts are not released. So d...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.