Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #86 – How to Load External Images

In Tip of the Day #84 I discussed the importance of being able to load controls from server side components (DLL’s) on demand rather than packaging everything together for one large client side download. The same principle applies to images. Let’s say your game has a large library of images that it needs for the game. Not all of these images will be needed at the start of the game. In this tip I will show you how to dynamically load these image resources from the server as needed.

To download an image from a server start by creating a BitmapImage. You will need to add a using statement to reference System.Windows.Media.Imaging. After you have created a BitmapImage point its UriSource to the absolute path of the image file you want to download from the Internet. To illustrate this I have used http://www.silverligthdev.net/images/myImage.png. Next, set the Source of your Image control to point to the BitmapImage.

Example XAML that contains the Image you will load:

<Canvas x:Name="LayoutRoot" Background="White">
    <Image x:Name="MyImage"></Image>
</Canvas>

The following C# code shows you how to load the image source from a file on a server. When the image is 100% loaded the event ImageOpened is fired.

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
 
        BitmapImage bi = new BitmapImage();
        bi.UriSource = new Uri("http://www.silverlightdev.net/images/blogImages/Sample.png");
        MyImage.Source = bi;
 
        MyImage.ImageOpened += new EventHandler<RoutedEventArgs>(MyImage_ImageOpened);
    }
 
    void MyImage_ImageOpened(object sender, RoutedEventArgs e)
    {
        // Image load complete.
    }
}

Thank you,

--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

In Tip of the Day #84 I discussed the importance of being able to load controls from server side components

# January 22, 2009 8:36 PM

spo81rty said:

How do we package the images in the xap?

# January 22, 2009 9:27 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# January 22, 2009 11:58 PM

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

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

# January 23, 2009 10:37 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

# January 23, 2009 7:21 PM

Community Blogs said:

In this issue: Jonathan van de Veen, David Justice, Andrej Tozon, Patrick Keating, Timmy Kokke, Shawn

# January 24, 2009 4:10 PM

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:

In Tip of the Day #86 I showed you how to load external images not included in the project such as images

# February 9, 2009 6:34 PM

Silverlight Travel » How to Load Images from a Stream said:

Pingback from  Silverlight Travel &raquo; How to Load Images from a Stream

# February 10, 2009 1:02 AM

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

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:35 PM