Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 774
Stephen Djohan Avatar asked Aug 31 '15 15:08

Stephen Djohan


People also ask

How do you change the source of an image in UWP?

Here's how to set the source to an image from the app package. Image img = new Image(); BitmapImage bitmapImage = new BitmapImage(); Uri uri = new Uri("ms-appx:///Assets/Logo.png"); bitmapImage. UriSource = uri; img. Source = bitmapImage; // OR Image img = new Image(); img.

How do I add an image to XAML UWP?

Go to tool box and drag and drop the image into the designer page. In the solution explorer go to assets and right click. Then click add -> existing item. Browse the required file.


1 Answers

You can also use it with BaseUri.

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

Assets is a folder name and you can change it with your any custom folder name :)

like image 54
Zia Ur Rahman Avatar answered Oct 12 '22 06:10

Zia Ur Rahman