Silverlight Windows Phone 7: Load Images From URL

好久不见. 提交于 2019-12-31 21:21:34

问题


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


回答1:


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



回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/2455195/silverlight-windows-phone-7-load-images-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!