简体   繁体   中英

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.

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);

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