简体   繁体   中英

Unable to create BitmapImage from Pictures library using UriSource

I have WindowsPhone 8.1 RT application. I want to create a simple BitmapImage and display it on the UI.

The image is saved in the Screenshots folder of the pictures library. When I try to create the BitmapImage using the following code, it always fails and empty area is displayed.

string imageLocation = "C:\\Data\\Users\\Public\\Pictures\\Screenshots\\wp_ss_20150923_0001.png"
BitmapImage bitmapImage = new BitmapImage(new Uri(imageLocation), UriKind.Absolute);
imageControl.Source = bitmapImage;

But when I read the storage file and create a stream, and set it using SetSourceAsync , the image is displayed.

StorageFile imageFile = await StorageFile.GetFileAsync(imageLocation);
var stream = await imageFile.OpenAsync(FileAccessMode.Read);
await bitmapImage.SetSourceAsync(bitmapImage);
imageControl.Source = bitmapImage;

Why can't I see the image being loaded in the image control when I use the Uri method to set image source?

Your application is "sandboxed", meaning it can not directly access the file system. And the Pictures library doesn't provide any URI-schema for accessing the images.

There's an another quite similar question on SO .

Here's some more information regarding file access on MSDN in WinRT. You may find the "Access media libraries" especially interesting.

You should try this

new Image() { Source = new BitmapImage(new Uri("ms-appx:///Assets/example.png")) })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM