Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #88 – How to handle image AG_E_NETWORK_ERROR errors

If you have ever worked with images while developing and running Silverlight app you have more than likely come across the AG_E_NETWORK_ERROR  exception. This exception is thrown if an image was not able to load for whatever reason (I.e. it couldn’t be found, corrupt, etc.).

In order to prevent your users from seeing this error it’s important to monitor for the ImageFailed event for each image your try to load.

The following code demo’s how to accomplish this for an image.

public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
        LoadImage("http://www.mysite.com/grass.png");
    }
 
    private void LoadImage(string url)
    {
        Image img = new Image();
        img.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
        Uri imageUri = new Uri(url, UriKind.RelativeOrAbsolute);
        ImageSource imgSource = new BitmapImage(imageUri);
        img.SetValue(Image.SourceProperty, imgSource);
        LayoutRoot.Children.Add(img);
    }
 
 
    void img_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
        // Handle the error here
    }

In the img_ImageFailed() event add the custom code you will need to handle this error.

 

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

If you have ever worked with images while developing and running Silverlight app you have more than likely

# January 27, 2009 11:45 AM

vitor_canova said:

exactly what I need. Becouse the TIP #86. Thank`s a lot.

# January 27, 2009 6:26 PM

VixMVP said:

Hope the same solution work for Video files also,surprise me atleast, sometimes when file get attached locally to media or image locally not work out from VS when I set source but same works well via blend.

Thanks for this tip,very useful since AG_E_NETWORK_ERROR might drive users crazy.

# January 28, 2009 12:09 AM

Community Blogs said:

In this issue: Thiago Felix, Thomas Kirchmair, Ken Cox, Jeff Paries, Daniel Crenna, Bryant Likes, Mike

# January 28, 2009 12:30 AM

Dew Drop - January 28, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - January 28, 2009 | Alvin Ashcraft's Morning Dew

# January 28, 2009 8:48 AM

Programming with Silverlight, WPF & .NET » Tip of The Day #84 - #89 How to …. said:

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Tip of The Day #84 - #89 How to &#8230;.

# January 29, 2009 2:27 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# February 10, 2009 6:22 PM

Visual Web Developer Team Blog said:

Most Recent Posts: Tip #93 - Reading XML with Silverlight Tip #92 - How to Load Images from a Stream

# February 10, 2009 6:25 PM

orangechicken said:

Is there a way to determine what URL it's actually trying to load and what the actual error is? The exception that's raised has no useful information.

# March 29, 2009 8:32 PM

mike.snow said:

The only way to know what image failed to load (that I know of) is to monitor the ImageFailed event for each image you load.

# March 30, 2009 11:40 AM

Error Exception said:

Silverlight Error: AG_E_NETWORK_ERROR

# May 1, 2009 2:16 PM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:35 PM