Search code examples
c#xamlwin-universal-app

UWP - Image Uri in Application Folder


I'm having a little issue here in showing images.

So when I'm trying to load images from XAML, I can use a relative uri to the image source like this :

<Image Source="/Assets/image.jpg" />

But if I try to change the image source programatically from code behind, I always get an exception and I'm sure it's because of the false Uri. I tried something like this :

BitmapImage bitmapImage = new BitmapImage(new Uri("/Assets/image.jpg"));

Am I doing it wrong? Any help will be appreciated, thanks!


Solution

  • To access files stored inside the application package, but from code where there is no inferred root authority, you need to specify the ms-appx: scheme :

    So in your case it will be something like :

    BitmapImage bitmapImage = 
                         new BitmapImage(new Uri("ms-appx:///[project-name]/Assets/image.jpg"));
    

    Read this documentation for more details : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965322.aspx

    Hope it helps.