csomakk
csomakk

Reputation: 5497

Image relative path works in one project, but not in other

The code below works in another project. Also in the second one if I include absolute url. I have the res folder in my project folder. What can be different?

 BitmapImage myBitmapImage = new BitmapImage();
         myBitmapImage.BeginInit();
         myBitmapImage.UriSource = new Uri(@"\res\arato.png", UriKind.RelativeOrAbsolute);
         myBitmapImage.EndInit();

         var img = new Image();
         img.Source = myBitmapImage;
         img.Margin = new Thickness(0, 0, 0, 0);

         canvas.Children.Add(img);

Upvotes: 1

Views: 431

Answers (3)

Bill Tarbell
Bill Tarbell

Reputation: 5234

This likely has to do with the build action on the file. If you view the properties on the file inside of Visual Studio you should see Build Action. Wpf plays best with "Resource" and doesn't work as well with "Embedded Resource". Sadly, Embedded Resource has the more intuitive name (at least to me), so I had trouble with this when i was first learning WPF.

Upvotes: 0

csomakk
csomakk

Reputation: 5497

The solution was: I had to drag the pictures to the folder in Visual Studio, not in File Explorer.

Upvotes: 0

Rohit Vats
Rohit Vats

Reputation: 81233

Use Pack URIs to give the relative or absolute path.

Upvotes: 1

Related Questions