简体   繁体   中英

How to display an image from web in wp7?

I have a page in my wp7 application which need to display an image in it. I have a url array. It may contain 0 to 500 urls. If no url present in the array then a message shows "No Images". if only one url is in the url array then the image should be displayed. If it contains more than one url then I need to display the image corresponds to the first url and a next button in the page. If I pressed the next button the second image will load and then a back button should be displayed. The image may have larger size then scrolling should be enabled.

How can I do this?

When I am trying to load the image in a WebBrowser, I got an error "You cannot call WebBrowser methods until it is in the visual tree."

Try this one,

public void DownloadImages()
{

    HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create(your_url);
    reqest.BeginGetResponse(DownloadImageCallback, reqest1);
}


void DownloadImageCallback(IAsyncResult result)
{
     HttpWebRequest req = (HttpWebRequest)result.AsyncState;
     HttpWebResponse responce = (HttpWebResponse)req1.EndGetResponse(result);
     Stream s = responce.GetResponseStream();
     Deployment.Current.Dispatcher.BeginInvoke(() =>
     {
         bmp = new BitmapImage();
         bmp.SetSource(s);
     });
}

bmp(bitmapimage) assigned as imagesource. In this way You can download image from server.

It is much easier to use WebClient instead of HttpWebRequest.

public void LoadImage(string uri)
{
    WebClient wc = new WebClient();
    wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
    wc.OpenReadAsync(new Uri(uri));
}

private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    BitmapImage bi = new BitmapImage();
    bi.SetSource(e.Result);             // Here, you got your image
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM