Loading images in Silverlight 2
June 30, 2008 @ 11:29 pm in Microsoft, Silverlight
Reading through the Silverlight.net forum I still see a ton of people struggling with how to load an image procedurally. I thought it would be beneficial to included a quick sample of four different ways you can set the Image.Source property within Silverlight 2.
Image 1 – Resource Loaded via XAML – The easiest and most common way to load an image is to include the image in your project and set its "Build Action" to Resource. This will embedded the image within the primary assembly which gets packaged in your projects XAP file. Here is what that assembly looks like if you explore it with Reflector.
To reference this embedded resource within xaml, you can set the source attribute equal to the following
<Image Source="ImageSource;component/1.jpg"…>
Image 2 – Resource Loaded procedurally – In the event that you need to swap or load an images source procedurally, you can simply load up a BitmapImage via a relative URI and set the Image.Source equal to this object.
//Image 2 - Loaded via bitmap image data // - Build Action=Resource // - Copy to Output Directory = Do Not Copy BitmapImage bmpImg2 = new BitmapImage(); Uri uri2 = new Uri("2.jpg", UriKind.Relative); bmpImg2.UriSource = uri2; image2.Source = bmpImg2;
Image 3 – Build Action = Content – If you do not desire to embed an image within your assembly, you can set its build action to “Content” and that image will be packaged within the XAP.
//Image 3 - Load image that is included in the XAP file // - Build action = Content // - Copy to Output Directory = Do Not Copy BitmapImage bmpImg3 = new BitmapImage(); Uri uri3 = new Uri("/3.jpg", UriKind.Relative); bmpImg3.UriSource = uri3; image3.Source = bmpImg3;
(Note: The image file name in the URI is proceeded with a front slash (/). This is required to indicate that the image is relative to the location of the assembly.)
Image 4 – Build Action = None / Copy to Output Directory = Copy always - In the instance that you don’t want the image to be downloaded locally (packaged in XAP), you can set the “Build Action” equal to “None” and set the “Copy to Output Directory” equal to “Copy always”.
Using the “TestPage.html” page which is automatically generated in your projects “bin” directory you can reference the image location relatively, just as you did in sample 3.
//Image 4 - Load image that is located at some relative url // - Build action = None // - Copy to Output Directory = Copy always // (Note: I had to manually copy the image to // my web projects ClientBin directory) BitmapImage bmpImg4 = new BitmapImage(); Uri uri4 = new Uri("/4.jpg", UriKind.Relative); bmpImg4.UriSource = uri4; image4.Source = bmpImg4;
Code: ImageSource.zip
/p>
Tagged as 

July 2nd, 2008 at 1:52 pm
You mentioned loading an image procedurally using BitmapImage with a URI, but there’s another important way to load an image. You can use the BitmapImage.SetSource method to load an image from a stream. The stream could be a local file specified via the open file dialog, a remote file downloaded in some way, data from a database, an image generated on the fly (see Stegman’s PNG encoder), and so on.
July 28th, 2008 at 10:37 am
Could add some info on the tradeoffs (scenarios) WHEN i would use #1 (in assy), #2, #3 (in xap, not in assy), #4 (not in xap, in same folder on webserver)
I guess #4 allows faster xap download if jpg not needed right away.
August 28th, 2008 at 2:08 pm
BitmapImages is from namespace System.Windows.Media.Imaging
November 12th, 2008 at 2:18 am
Thanks, it helps me
January 29th, 2009 at 1:49 am
Thank you so much.It helps me.
March 24th, 2009 at 9:28 am
Is there a working example of this????