1

I'm trying to load an image from a URL in Silverlight and have followed the steps at this site but to no avail.

My code is as follows:

 imageUri = new Uri("http://php.scripts.psu.edu/dept/iit/hbg/philanthropy/Images/BlueSkyLarge.jpg", UriKind.Absolute);
 System.Windows.Media.Imaging.BitmapImage bi = new    System.Windows.Media.Imaging.BitmapImage();
 bi.UriSource = imageUri;
 m_Image.Source = bi;

 m_Image.ImageOpened += new EventHandler<RoutedEventArgs>(Image_Opened);

The callback function (Image_Opened) is never called either..

teedyay
  • 23,293
  • 19
  • 66
  • 73
meds
  • 21,699
  • 37
  • 163
  • 314

2 Answers2

3

Is your Silverlight application running from the domain php.scripts.psu.edu? If not, Silverlight will block access to it because it will not allow TCP requests made to any domain other than the one the application was loaded from.

See here for network restrictions in Silverlight.

EDIT: commenter is right. It's the cross-zone issue you're seeing now. Here's a link with a table indicating what an Image (among others) can and can't do.

Adam Sills
  • 16,896
  • 6
  • 51
  • 56
  • Oh so there is absolutely no way to have, for example, someone enter an image URL into a silverlight application and have it load up the image unless the url of the image was the same as silverlights hosted domain? – meds Sep 27 '10 at 13:54
  • There are 2 ways to do it: 1) have an trusted out of browser application installed so the network restrictions are lifted or 2) the external domain you are loading must have one of two silverlight policy files installed at its root. Both of those are described in that link. – Adam Sills Sep 27 '10 at 13:57
  • Alternately you can build your own IHttpHandler that takes an image URL, downloads it and writes it to its output stream. Then use *that* as your URL. – Adam Sills Sep 27 '10 at 14:04
  • Ok thanks, I'll take a look at the link :) Just wanted to make sure I wasn't chasing a wild goose. – meds Sep 27 '10 at 14:05
  • cross domain restrictions do not apply to the images, cross zone acess will prevent your image from loading. – Denis Sep 27 '10 at 18:57
  • @AdamSills, can you explain more on `Alternately you can build your own IHttpHandler that takes an image URL, downloads it and writes it to its output stream. Then use that as your URL.` How to build the IHttpHandler in Silverlight? – Peter Lee Nov 18 '12 at 22:36
  • Keep Googling. http://www.techrepublic.com/blog/programming-and-development/download-files-over-the-web-with-nets-webclient-class/695 http://stackoverflow.com/questions/21877/dynamically-rendering-aspimage-from-blob-entry-in-asp-net – Adam Sills Nov 18 '12 at 23:47
-1

Another thing I would fix in your code is that you attach the handler at the end. In theory the event handler may not be called if the image is loaded really fast.

I suppose it would not happen on most cases, but with caching/object reuse/etc. who knows. I would attach the handler just after instantiating the object and be safe.

Francesco De Vittori
  • 9,100
  • 6
  • 33
  • 43