Problem adding C#images

Asked

Viewed 156 times

0

I’m trying to create an object like Image from an existing image, which will be inserted into a Frame and which in turn will be inserted into a Stackpanel. However, only images/icons that are inserted within the project are displayed in the component (#2). I did tests to check if it is the file that does not exist, but does not enter (#1).

Does anyone know what it is? All files are with access permission 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);     
  • I was able to solve my problem with the video: https://www.youtube.com/watch?v=Nb89r13Z1q4 Grateful :)

2 answers

1

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

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

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

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

0

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

if (!File.Exists("C:\\Users\\32-add.png")) MessageBox.Show("");
  • 3

    Shouldn’t the backslash escape? To prevent that \n is interpreted as line breaking?

  • If I leave only one for path definition, it returns me text representation errors for UTF 16(?), I use the backslash for being like I use for path location with my icons added to the project, and which are successfully recognized... I don’t know if this has anything to do with it, but it’s WPF

  • @Jeffersonquesado That’s right. I corrected the answer based on your observation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.