WinRT: How to read images from the pictures library via an URI?

六月ゝ 毕业季﹏ 提交于 2019-12-24 14:32:01

问题


Trying to read an image that is stored in the pictures library via an URI the image is never displayed (in an Image control). Reading the same image via a stream works (assuming the app hat the Picture Library capability declared of course). Reading images from the application's data folder via an URI works.

Does someone know what could be wrong?

Here is how I (unsucessfully) try to read an image via an URI:

var imageFile = (await KnownFolders.PicturesLibrary.GetFilesAsync()).FirstOrDefault();
string imagePath = imageFile.Path;
Uri uriSource = new Uri(imagePath);
var bitmap = new BitmapImage(uriSource);
this.Image.Source = bitmap;

Here is how I sucessfully read the same image via a stream:

var imageFile = (await KnownFolders.PicturesLibrary.GetFilesAsync()).FirstOrDefault();
BitmapImage bitmap;
using (var stream = await imageFile.OpenReadAsync())
{
   bitmap = new BitmapImage();
   await bitmap.SetSourceAsync(stream);
}
this.Image.Source = bitmap;

I need to read the image via URI because this is the fastest way to read images and is async by nature, working perfectly with data binding.


回答1:


There is no URI for the pictures library. You'll need to get the StorageFile and stream it in.

The file URI you use doesn't work because the app doesn't have direct access to the PicturesLibrary and so cannot reference items there by path. The StorageFile object provides brokered access to locations that the app doesn't natively have permissions to.



来源:https://stackoverflow.com/questions/30245310/winrt-how-to-read-images-from-the-pictures-library-via-an-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!