views:

50

answers:

2

Hi All,

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..

+3  A: 

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
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
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
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
Ok thanks, I'll take a look at the link :) Just wanted to make sure I wasn't chasing a wild goose.
meds
cross domain restrictions do not apply to the images, cross zone acess will prevent your image from loading.
Denis
A: 

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
Care to explain why the negative vote?
Francesco De Vittori