Search code examples
c#wpfimageimagesource

Set imagesource of wpf image


I know that there are a few similiar questions but they don´t help me:

Have got the following code to show a Image in code behind:

BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new `Uri("pack://application:,,,/ApplicationName;component/Images/Klar.JPG");`
logo.EndInit();

The image is in the Images folder at the same level as the bin folder.

What is my mistake?


Solution

  • For information about Pack URIs see here. I believe yours should be:

    logo.BeginInit();
    logo.UriSource = new Uri("pack://application:,,,/Images/Klar.JPG");
    logo.EndInit();
    

    which assumes that at the project level you have a folder called Images which contains a jpg called Klar.JPG.