4

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

Image is in Assets folder. Here is my XAML code:

<Page
    x:Class="MyApp.WelcomeScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"  Width="800" Height="480" RequestedTheme="Dark">

    <Grid Background="#C4043F">
        <Image Name="MyPicture" Source="/Assets/LOGO.png" HorizontalAlignment="Left" VerticalAlignment="Top"/>
   </Grid>
</Page>

I have also tried set source programatically like this and Image is not shown. I have also tried putting try-catch block but exception is not thrown.

string imagePath = "ms-appx:///Assets/LOGO.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imagePath));
MyPicture.Source = bitmapImage;

I have already tried everything that is described here: UWP - Image Uri in Application Folder

What am I missing?

KOmrAD
  • 93
  • 5
  • 14
  • Either the relative path is wrong, or the image is not an `EmbeddedResource`. – Stefan Over Nov 03 '17 at 11:59
  • Indeed, if the path to the image is correct, just setting should work – Depechie Nov 03 '17 at 12:05
  • There is deffinitely something wrong with the path. if I add , it works. The Project name in Visual Studio and the folder where the project is saved have different names. Maybe that is what is bothering the Visual Studio... – KOmrAD Nov 03 '17 at 12:17
  • try `Source="ms-appx:///YourProjectName/Assets/LOGO.png"` – Jet Chopper Nov 03 '17 at 13:03
  • 1
    it is only item in your grid ? try give height and width for checking purpose. "Check build action is set to content and and try to set always copy to output directory" in image properties – Shubham Sahu Nov 03 '17 at 13:21
  • @ShubhamSahu that solved my problem. You can post as answer so I can accept your solution. All I did was "Content" and "Always copy" and it worked. Thank you. – KOmrAD Nov 03 '17 at 13:54
  • @KOmrAD ;) Okay , Glad it works – Shubham Sahu Nov 03 '17 at 14:00
  • I try your code and I can see image. – lindexi Nov 04 '17 at 03:06

1 Answers1

5

Follow this step:

1) Build action is set to content.

2) And try to set always **copy to output directory" in your image properties.

Screenshot

Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34