我有带有图像URL的字符串数组
Array中的示例图像:
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但不接受因为con't Implement string to system.windows.media.imagesource
答案 0 :(得分: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;
Here更多信息。
修改强>
这有可能对远程图像不起作用(目前无法测试),我相信在这种情况下你需要下载图像,所以这是你如何做到的:
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;