WinRT:如何通过 URI 从图片库中读取图像? [英] WinRT: How to read images from the pictures library via an URI?

查看:24
本文介绍了WinRT:如何通过 URI 从图片库中读取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过 URI 读取存储在图片库中的图像,该图像永远不会显示(在 Image 控件中).通过流读取相同的图像有效(假设应用程序当然声明了图片库功能).通过 URI 从应用程序的数据文件夹中读取图像有效.

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?

以下是我(未成功)尝试通过 URI 读取图像的方法:

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;

我需要通过 URI 读取图像,因为这是读取图像的最快方式,并且本质上是异步的,与数据绑定完美配合.

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.

推荐答案

图片库没有 URI.您需要获取 StorageFile 并将其流式传输.

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

您使用的文件 URI 不起作用,因为该应用无法直接访问 PicturesLibrary,因此无法通过路径引用其中的项目.StorageFile 对象提供对应用本身无权访问的位置的代理访问.

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.

这篇关于WinRT:如何通过 URI 从图片库中读取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆