views:

13413

answers:

8

I'm tinkering with Silverlight 2.0.

I have some images, which I currently have a static URL for the image source. Is there a way to dynamically load the image from a URL path for the site that is hosting the control?

Alternatively, a configuration setting, stored in a single place, that holds the base path for the URL, so that each image only holds the filename?

+4  A: 

In the code behind or a value converter you can do

  Uri uri = new Uri("http://testsvr.com/hello.jpg");
  YourImage.Source = new BitmapImage(uri);
Aaron Fischer
A: 

SilverlightHost.Source will provide you the URL that was used to load the XAP file. You can use this to then construct a relative URL for your images.

So if for example your XAP is hosted on http://foo.bar/ClientBin/bas.xap and your images were stored in http://foo.bar/Images/ you can simply use the Source to grab the host name and protocol to construct the new URI.

Aaron Weiker
+4  A: 

From what I gather you aren't trying to change the image itself dynamically, but rather to correctly determine the location of the image at runtime.

I believe simply prefixing the image relative URL with "../" should get you to the root of your application, not necessarily the site as the application might not be hosted in the root of a site.

If your XAP file is located as follows:

http://somesite.foo/app1/somethingelse/clientbin/MyFoo.xap

And you where trying to link the following image:

http://somesite.foo/app1/somethingelse/images/a/boo.png

Apparently all relative URI's are relative to where the XAP file is located (ClientBin folder typically) and Silverlight appends the current Silverlight client namespace. So if you Silverlight control is in the namespace Whoppa you would need to put all your images in the clientbin/Whoppa/ directory. Not exactly convenient.

The workaround is to use absolute URIs as follows:

new Uri(App.Current.Host.Source, "../images/a/boo.png");

Craig Nicholson
This doesn't work for me. The only way I can get an image to load is to specify the full URL, like: http://localhost/hello.jpg. No image shows up if I use: hello.jpg, /hello.jpg, or ../hello.jpg. And my .xap file is in the /ClientBin folder.
Scott Mitchell
Apparently all relative URI's are relative to where the XAP file is located (ClientBin folder typically) and Silverlight appends the namespace. The workaround is to go absolute as follows: new Uri(App.Current.Host.Source, "../hello.jpg");
Craig Nicholson
A: 

img.Source = new BitmapImage(image uri) must work.

NinethSense
A: 

img.Source = new BitmapImage(new Uri("/images/my-image.jpg", UriKind.Relative)); will properly resolve to the root of the Silverlight application where as "../images/my-image.jpg" will not.

This is only true in the code-behind when dynamically setting the source of the image. You cannot use this notation (the "/" to designate the root) in the XAML (go fiquire, hope they fix that)

mknopf
A: 

below worked for me only when image is included in the project as a resource file.

img.Source = new BitmapImage(new Uri("/images/my-image.jpg", UriKind.Relative));

I am unable to access url from absolute URLs. Not even flickr's farm url for images.

A: 

http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx

using System.Windows.Resources; // StreamResourceInfo using System.Windows.Media.Imaging; // BitmapImage ....

StreamResourceInfo sr = Application.GetResourceStream( new Uri("SilverlightApplication1;component/MyImage.png", UriKind.Relative)); BitmapImage bmp = new BitmapImage(); bmp.SetSource(sr.Stream);

Dan Wygant
A: 
// create a new image
Image image = new Image();

// better to keep this in a global config singleton
string hostName = Application.Current.Host.Source.Host;                   
if (Application.Current.Host.Source.Port != 80)
    hostName += ":" + Application.Current.Host.Source.Port;

// set the image source
image.Source = new BitmapImage(new Uri("http://" + hostName + "/sexy_lesbians112.jpg", UriKind.Absolute));  
Malcolm Swaine