views:

675

answers:

4

Hi,

I got the code below that is trying to load an image from the web into an Image control, when I run it I get an error on the given line that no network access is allowed:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient webClientImgDownloader = new WebClient();
            webClientImgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted);
            webClientImgDownloader.OpenReadAsync(new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute));
        }

        void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.Result); // ERROR HERE!
            image1.Source = bitmap;
        }

Silverlight for Windows Phone 7

+2  A: 

I see you're retrieving the image from Dilbert.com does that site have a cross domain policy file?

Graeme Bradbury
Windows Phone 7 Silverlight applications don't need a Cross Domain Policy this restriction is removed, this was not known when this question and reply was posted, thought this may be useful to others - it still doesn't support GIF though.
RoguePlanetoid
A: 

Can you give us the full exception stack trace? the error could be that your phone emulator does not have internet access, or it could be the image on the dilbert server that does not allow anonymous requests that did not originate from their site ... so guidance on a solution will differ :-)

Joel Martinez
+2  A: 

Trying to download content with WebClient will require a client access policy file to be present on the source server. For images you can avoid this requirement by doing it like this:-

private void button1_Click(object sender, RoutedEventArgs e)
{
    Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
    image1.Source = new BitmapImage(uri);
}
AnthonyWJones
+1, but does this still work since the image is a .GIF?
Jacob
@Jacob: At the time I didn't notice is was a Gif. The answer is no it gifs aren't supported. It does work with Lennie's modified URL.
AnthonyWJones
+1  A: 

Silverlight doesn't support GIF only JPG, so I wrote:

www.lenniedevilliers.net/displaygif.aspx?link=http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif

the displaygif.aspx page convert the GIF into a JPG.

Lennie De Villiers
That will also get round the client access policy issue if you still feel it necessary to use WebClient.
AnthonyWJones