Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #10 – How to Dynamically Load and Display Images

Loading images in Silverlight is fairly straight forward. The first step is to create what’s called a Uniform Resource Identifier (URI). The URI is essentially a string that points to a resource. The resource can be locally in the project or out on the Internet. Images that are loaded locally must be first added to your Visual Studio project.

Image image = new Image();
Uri uri = new Uri("images/myImage.png", UriKind.Relative);
ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
image.SetValue(Image.SourceProperty, img)

Now, to display the image you will need to add it to the Children of an element that you have declared in your XAML. For example, let’s say you have created a Canvas object in your Page.xaml under the parent Grid object. Using the x:Name tag, give it the name “Map”:

<Grid x:Name="MainGrid">
  <Canvas x:Name="Map">
   </Canvas>
</Grid>

Now, back in your Page.xaml.cs file you can dynamically add the image you just created to the canvas object like this:

Map.Children.Add(image);

Btw, declaring an image in your XAML works the same way. Example:

<Image Source="images/MyImage.png"></Image>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

BenHayat said:

Mike, great posts. Please keep them coming.

Thank you!

..Ben

# June 6, 2008 1:48 PM

BenHayat said:

So Mike, you're saying, if I don't include my images as part of my XAP file and then at runtime, I can grab and load one image at a time from the server and show it on the SL client with the above technique?

..Ben

# June 6, 2008 1:51 PM

robhouweling said:

Hi Mike,

Is it possible to read the width and height of a dynamically loaded image in SL2? I know there are problems with this in SL2b1.

Kind regards,

Rob

# June 6, 2008 1:55 PM

mike.snow said:

Ben-

I was saying you should include them in your project, this will then package them into your XAP. For example, from your solution explorer right click on your Silverlight Application node, choose Add Existing item, browse to the image you want to add it to the project.

Rob-

To get the width and height of an image you have to wait until the image is fully loaded. Since Silverlight is asynchronous you cant get the width/height instantly after loading. I will post a tutorial on how to do this since it's a question frequently asked.

# June 6, 2008 2:43 PM

sladapter said:

Then how do we do it in Xaml? It used to be in Xaml you set the Source to the file URL. We can no longer do that?

# June 6, 2008 4:10 PM

DZaK said:

What about MediaElement (video files)? There is no such think, like VideoSource, what should we put to

mediaElement.SetValue(MediaElement.SourceProperty, here);  ?

Bests,

Jacek

# June 6, 2008 6:06 PM

Eric Willeke said:

Mike, have you experienced problems in Beta 1 where using a Uri as a source on a BitmapImage fails once the image has been displayed? (i.e. Set a "loading" image, then set the Uri with the intent of it replacing the loading image when it's done downloading) If so, does this problem go away in Beta 2?

Up until now, I've had to use configure the BitmapImage utilizing the SetSource( Stream ) method and doing my own download using a WebClient instance.

# June 6, 2008 7:15 PM

Dew Drop – June 7, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop &ndash; June 7, 2008 | Alvin Ashcraft's Morning Dew

# June 7, 2008 5:02 PM

private asteroid said:

Updating Projects

# June 8, 2008 11:27 AM

mike.snow said:

Jacek- I am not certain about MediaElement but I will try to look into it.

Eric- Unfortunately I haven't seen what you are describing. Can you post some sample code?

# June 13, 2008 4:33 PM

Visual Web Developer Team Blog said:

Interested in learning something new about Silverlight almost every day? I will be posting “Tips of the

# July 1, 2008 1:54 PM

14 Silverlight Tips | DavideZordan.net said:

Pingback from  14 Silverlight Tips | DavideZordan.net

# July 2, 2008 4:31 AM

private asteroid said:

Updating Projects

# November 1, 2008 9:55 AM