pearcewg
pearcewg

Reputation: 9613

Silverlight image: load URL dynamically?

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?

Upvotes: 13

Views: 43028

Answers (8)

JewelThief
JewelThief

Reputation:

The below code worked for me only when the 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.

Upvotes: 2

Dan Wygant
Dan Wygant

Reputation: 51

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);

Upvotes: 4

Malcolm Swaine
Malcolm Swaine

Reputation: 2258

// 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 + "/cute_kitten112.jpg", UriKind.Absolute));  

Upvotes: 6

Craig Nicholson
Craig Nicholson

Reputation: 1241

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");

Upvotes: 16

mknopf
mknopf

Reputation: 380

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)

Upvotes: 2

NinethSense
NinethSense

Reputation: 9028

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

Upvotes: 2

Aaron Weiker
Aaron Weiker

Reputation: 2531

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.

Upvotes: 2

Aaron Fischer
Aaron Fischer

Reputation: 21211

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);

Upvotes: 15

Related Questions