3

I have String Array with Image Url

Sample Image from Array:

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

Now i need to Bind Images in Xaml

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

trying to Give img.source But Not Accepting because con't Implement string to system.windows.media.imagesource

1
  • If you are binding you CAN use string. Where is the string (url) stored? what is the DataContext of the Image control? Commented Jul 25, 2013 at 16:29

1 Answer 1

5

Did you try setting 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 is a bit more info.

EDIT

There is a possibility that this wont work for remote images (can't test it at the moment), I believe in such cases you need to download the image, so here is how you do it:

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I try this Same error string to system.windows.media.imagesource error
@SriramSatti In case this doesn't work for remote images, I've added another approach.
Thanks for Al Your Replys... I got that with Single line of Code img.Source = new BitmapImage(new Uri("fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/…", UriKind.RelativeOrAbsolute));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.