Jolof
Jolof

Reputation: 41

C# UWP displaying Image

I have a little problem. I have to display Images from PicturesLibrary. I have this code:

    info = new ImageFileInfo() { ImageTitle = file.Path, ImageType = file.DisplayType, Image = file.Path };

I tried using converter:

  return new Uri("ms-appx:///" + imagename, UriKind.Absolute);

But my images dont show up. I know that there is a way to display images from outside of projects, cause I have been using it some time ago, but I forgot how it was done, and I can't find it anywhere.

Upvotes: 0

Views: 73

Answers (1)

Anran Zhang
Anran Zhang

Reputation: 7737

The Uri schemes of ms-appx are only used for the files in the project, and the files in the picture library cannot be obtained through it.

In UWP, we do not recommend using the file path to get file. Because UWP has strict restrictions on accessing files from the path.

If you want to access files from the picture library, first you need to check Picture Library in the Capability tab in package.appxmenifest.

Then you can access it by file name:

var img = await KnownFolders.PicturesLibrary.TryGetItemAsync("test.png");

But there are other more recommended way, like use Token to access files:

After obtaining the file, we store it in the future access list and obtain a Token

Save

string faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
info = new ImageFileInfo() { ImageTitle = file.Path, ImageType = file.DisplayType, Image = faToken };

Get

var image = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(info.Image);

Upvotes: 1

Related Questions