c# - 将图像源设置为 URI

标签 c# silverlight windows-phone-7

如果我有一个在线图像的链接,并且我想将图像源设置为这个 uri,我应该怎么做最好?我正在尝试的代码如下所示。
<Image Name="Poster" Height="400" Width="250" VerticalAlignment="Top" Margin="0,10,8,0"/>

BitmapImage imgSource = new BitmapImage();<br/> imgSource.UriSource = new Uri(movie.B_Poster, UriKind.Relative);<br/> Poster.Source = imgSource;

此外,如果我想缓存此图像以再次加载它,该怎么做?
谢谢

最佳答案

这是正确的做法。如果您想缓存图像供以后重复使用,您可以随时将其下载到独立存储中。将 WebClientOpenReadAsync 结合使用 - 传递图像 URI 并将其存储在本地。

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("IMAGE_URL"));

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("image.jpg", System.IO.FileMode.Create, file))
    {
        byte[] buffer = new byte[1024];
        while (e.Result.Read(buffer, 0, buffer.Length) > 0)
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}

阅读它会反过来:

using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("image.jpg", System.IO.FileMode.Open, file))
{
    BitmapImage image = new BitmapImage();
    image.SetSource(stream);

    image1.Source = image;
}

关于c# - 将图像源设置为 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6940880/

相关文章:

c# - 具有无限超时的 HttpClient 抛出超时异常

silverlight - 为什么重新安装 Silverlight 会破坏我的导入配置?

c# - 在 SharePoint 2007 中使用 Silverlight 4

c# - 如何使用 silverlight 和 ASP.NET MVC 测量上传和下载互联网速度

windows-phone-7 - 如何在 Windows Phone 7 中使用全局样式?

c# - 有没有办法找出您的应用程序在 WP7 上使用了多少独立存储空间?

c# - 在 WP7 上释放 Application.GetResourceStream 返回的底层 Stream

c# - 为什么 "Covariance"和 "Contravariance"的概念在实现接口(interface)的方法时适用?

c# - 使用DirectShow捕获音频

c# - 确定客户端的计算机名称