3

我有带有图像网址的字符串数组

来自数组的示例图像:

string Image = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg";

现在我需要在 Xaml 中绑定图像

<Image Name="img" HorizontalAlignment="Left" VerticalAlignment="Top" Width="66" Height="66" Source="{Binding Image} " />

尝试提供 img.source 但不接受,因为未将字符串实现到 system.windows.media.imagesource

4

1 回答 1

5

您是否尝试设置Source

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg");;
bitmapImage.EndInit();

img.Source = bitmapImage;

是更多信息。

编辑

有可能这不适用于远程图像(目前无法测试),我相信在这种情况下您需要下载图像,所以这是您的操作方法:

var imgUrl = new Uri("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg");
var imageData = new WebClient().DownloadData(imgUrl);

// or you can download it Async won't block your UI
// var imageData = await new WebClient().DownloadDataTaskAsync(imgUrl);

var bitmapImage = new BitmapImage {CacheOption = BitmapCacheOption.OnLoad};
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageData);
bitmapImage.EndInit();

return bitmapImage;
于 2013-07-25T04:14:47.347 回答