Problem when adding images C #

0

I'm trying to create an object of type Image from an existing image, which will be inserted into a Frame and in turn will be inserted into a > StackPanel . However, only images / icons that are inserted inside the project are displayed in the component (# 2). I tried to check if it is the file that does not exist, but it does not enter (# 1).

Does anyone know what it is? All files are allowed access for all users ...

if (!File.Exists("C:/Users/32-add.png")) MessageBox.Show("");
//teste para verificar a existência (#1)

//Uri imageUri = new Uri(photoPath + "32-star.png", UriKind.Relative);

//testando com um ícone do meu projeto, aqui vai (#2)

Uri imageUri = new Uri("C:/Users/32-add.png", UriKind.Relative);
//aqui não vai

BitmapImage imageBitmap = new BitmapImage(imageUri);
Image img = new Image {Source=imageBitmap, Width = 130, Height = 130, MinWidth = 130, MinHeight = 130};
//conversão

Frame frame = new Frame{Content = img};

stackPanel.Children.Add(frame);     
    
asked by anonymous 05.03.2018 / 23:02

2 answers

1

You can only use \ , but for this, put a @ in front of the string:

File.Exists(@"C:\Users-add.png")

And you can also use two \ , but without @ :

File.Exists("C:\Users\32-add.png")
    
06.03.2018 / 14:28
0

The problem is how you pass the image path. For absolute paths, you must use the backslash, getting:

if (!File.Exists("C:\Users\32-add.png")) MessageBox.Show("");
    
06.03.2018 / 00:37