如何在 wp7 中显示来自网络的图像? [英] How to display an image from web in wp7?

查看:20
本文介绍了如何在 wp7 中显示来自网络的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 wp7 应用程序中有一个页面需要在其中显示图像.我有一个 url 数组.它可能包含 0 到 500 个网址.如果数组中不存在 url,则会显示一条消息无图像".如果 url 数组中只有一个 url,则应显示图像.如果它包含多个 url,那么我需要显示对应于页面中第一个 url 和下一个按钮的图像.如果我按下下一个按钮,将加载第二个图像,然后应显示一个后退按钮.图像可能有更大的尺寸,则应启用滚动.

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.

我该怎么做?

当我尝试在 WebBrowser 中加载图像时,出现错误您无法调用 WebBrowser 方法,直到它位于可视化树中."

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

推荐答案

使用 WebClient 比使用 HttpWebRequest 要容易得多.

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
}

这篇关于如何在 wp7 中显示来自网络的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆