将图像源设置为URI

Ame*_*een 1 c# silverlight windows-phone-7

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

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

此外,如果我想缓存此图像再次加载它是如何完成的?
谢谢

Den*_*sky 5

这是正确的方法.如果要缓存映像以供以后重复使用,可以始终在隔离存储中下载它.使用WebClientwith OpenReadAsync- 传递图像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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

阅读它将是另一种方式:

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

    image1.Source = image;
}
Run Code Online (Sandbox Code Playgroud)