I am working on a Windows 8 app. I need to know how to programmatically set the Source of an Image. I assumed that the Silverlight approach would work. However, it doesn't. Does anybody know how to do this? The following will not work:
string pictureUrl = GetImageUrl();
Image image = new Image();
image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(pictureUrl, UriKind.Relative));
image.Stretch = Stretch.None;
image.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
image.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
I get an Exception that says: "The given System.Uri cannot be converted into a Windows.Foundation.Uri."
However, I can't seem to find the Windows.Foundation.Uri type.
This example uses a FileOpenPicker object to obtain the storage file. You can use whatever method you need to access your file as a StorageFile object.
Logo is the name of the image control.
Reference the following code:
Well,
Windows.Foundation.Uri
is documented like this:So the tricky bit isn't converting it into a
Windows.Foundation.Uri
yourself - it looks like WinRT does that for you. It looks like the problem is with the URI you're using. What is it relative to in this case? I suspect you really just need to find the right format for the URI.check your pictureUrl since it was what resulted in the exception.
but this should work as well
it should have nothing to do with Windows.Foundation.Uri. since winrt will handle it for you.
Try this format:
The given System.Uri cannot be converted into a Windows.Foundation.Uri
This is what I use: