Silverlight image: load URL dynamically?
Asked Answered
B

8

13

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?

Blacktail answered 24/10, 2008 at 2:37 Comment(0)
J
15

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);
Jankell answered 24/10, 2008 at 3:38 Comment(0)
B
16

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

Brunelleschi answered 25/10, 2008 at 12:57 Comment(2)
This doesn't work for me. The only way I can get an image to load is to specify the full URL, like: 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.Vitia
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");Brunelleschi
J
15

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);
Jankell answered 24/10, 2008 at 3:38 Comment(0)
M
6
// 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));  
Musselman answered 4/10, 2010 at 8:7 Comment(1)
nice image filename being inserted there :DBlacktail
B
4

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);
Begorra answered 13/5, 2010 at 16:4 Comment(0)
D
2

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.

Derouen answered 24/10, 2008 at 3:44 Comment(0)
R
2

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

Resigned answered 18/3, 2009 at 8:45 Comment(0)
U
2

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)

Utter answered 9/4, 2009 at 19:49 Comment(0)
S
2

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.

Saker answered 15/7, 2009 at 6:25 Comment(1)
You'll only be able to make http connections to resources on the server where your XAP lives unless you're running as a trusted applications, no?Peptize

© 2022 - 2024 — McMap. All rights reserved.