C# 将图像源设置为URI

C# 将图像源设置为URI,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,如果我有一个联机图像的链接,并且我想将图像源设置为这个uri,我应该如何做到最好?我正在尝试的代码如下所示。 BitmapImage imgSource=新的BitmapImage; imgSource.UriSource=新的Urimovie.B_海报,UriKind.Relative; Poster.Source=imgSource 另外,如果我想缓存此图像以再次加载它,这是如何完成的? 谢谢这是正确的方法。如果要缓存映像以供以后重新使用,可以始终将其下载到隔离存储中。将WebClient

如果我有一个联机图像的链接,并且我想将图像源设置为这个uri,我应该如何做到最好?我正在尝试的代码如下所示。

BitmapImage imgSource=新的BitmapImage; imgSource.UriSource=新的Urimovie.B_海报,UriKind.Relative; Poster.Source=imgSource

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

这是正确的方法。如果要缓存映像以供以后重新使用,可以始终将其下载到隔离存储中。将WebClient与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);
        }
    }
}
阅读它将是另一种方式:

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

    image1.Source = image;
}

这是正确的做法。如果要缓存映像以供以后重新使用,可以始终将其下载到隔离存储中。将WebClient与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);
        }
    }
}
阅读它将是另一种方式:

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

    image1.Source = image;
}

你做得对

要缓存图像,可以使用WebClient或WebRequest WebResponse机制将其下载到本地文件存储。然后,下次您要设置图像位置时,请检查它是否存在于本地。如果是,请将其设置为本地文件。如果没有,请将其设置为远程文件并下载


另外,您需要跟踪这些信息并删除旧文件,否则会很快填满手机的内存。

您做得很正确

要缓存图像,可以使用WebClient或WebRequest WebResponse机制将其下载到本地文件存储。然后,下次您要设置图像位置时,请检查它是否存在于本地。如果是,请将其设置为本地文件。如果没有,请将其设置为远程文件并下载


另外,您需要跟踪这些内容并删除旧文件,否则会很快填满手机内存。

您在代码中设置图像源的方式绝对不错。另一种选择是,如果使用binding/MVVM,则使用转换器将字符串URL转换为图像源:

public class StringToImageConverter : IValueConverter
{

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    string url = value as string;
    Uri uri = new Uri(url);
    return new BitmapImage(uri);
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

在代码隐藏中设置图像源代码的方式绝对不错。另一种选择是,如果使用binding/MVVM,则使用转换器将字符串URL转换为图像源:

public class StringToImageConverter : IValueConverter
{

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    string url = value as string;
    Uri uri = new Uri(url);
    return new BitmapImage(uri);
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

当我运行此程序时,会出现一个异常,提示:无法创建相对URI,因为'uriString'参数表示一个绝对URI。您传递的URI是什么,异常抛出的具体位置是哪里?这是我传递的URI:http://www.foo-web.net/res2/Adele Blanc-Sec.foob,例外情况发生在以下语句中:imgSource.UriSource=新的Urimovie.B_海报,UriKind.Relative;哦,很抱歉,我发现了这个问题,UriKind应该是绝对的,而不是相对的。再次感谢Dennis:您要传递的URI是绝对的-您应该使用UriKind.absolute。当我运行此操作时,我收到一个异常,它说:无法创建相对URI,因为'Uristing'参数表示绝对URI。您要传递的URI是什么,异常抛出的具体位置是哪里?这是我要传递的URI:http://www.foo-web.net/res2/Adele Blanc-Sec.foob,异常发生在以下语句中:imgSource.UriSource=new Urimovie.B_Poster,UriKind.Relative;哦,很抱歉,我发现了这个问题,UriKind应该是绝对的,而不是相对的。再次感谢Dennis:您传递的URI是绝对的-您应该使用UriKind.absolute。