无法使用 UriSource 从图片库创建 BitmapImage

Unable to create BitmapImage from Pictures library using UriSource

我有 WindowsPhone 8.1 RT 应用程序。我想创建一个简单的 BitmapImage 并将其显示在 UI.

图片保存在图片库的Screenshots文件夹中。当我尝试使用以下代码创建 BitmapImage 时,它​​总是失败并显示空白区域。

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;

但是当我读取存储文件并创建一个流,并使用 SetSourceAsync 设置它时,显示图像。

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

为什么我使用Uri方法设置图片来源时,在图片控件中看不到正在加载的图片?

您的应用程序是"sandboxed",这意味着它不能直接访问文件系统。图片库不提供任何用于访问图像的 URI 架构。

有一个

这里有一些关于 file access on MSDN in WinRT. You may find the "Access media libraries" 特别有趣的信息。

你应该试试这个

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