Application.GetResourceStream always return null

I've this project structure: https://i.stack.imgur.com/eAvrg.png

I'm trying to get the image from the Images folder in this way:

 StreamResourceInfo flag =
Application.GetResourceStream(
    new Uri($"/FamFamFam.Flags.Wpf;component/Images/{countryId.ToLower()}.png", UriKind.Relative));

I also tried to remove the application library prefix, and I tried also in this way:

var path = $"/FamFamFam.Flags.Wpf;component/Images/{countryId.ToLower()}.png";

the code is inside a Converter:

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var countryId = value as string;

        if (countryId == null)
            return null;

        try
        {
            StreamResourceInfo flag =
Application.GetResourceStream(
    new Uri($"/FamFamFam.Flags.Wpf;component/Images/{countryId.ToLower()}.png", UriKind.Relative));
            var path = $"/FamFamFam.Flags.Wpf;component/Images/{countryId.ToLower()}.png";
            var uri = new Uri(path, UriKind.Relative);
            var resourceStream = Application.GetResourceStream(uri);
            if (resourceStream == null)
                return null;

            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = resourceStream.Stream;
            bitmap.EndInit();
            return bitmap;
        }
        catch
        {
            return null;
        }
    }

both flag and path return null on the exception. What am I doing wrong?

1 answer

  • answered 2018-01-11 22:30 circular

    You need to mark the image files as Resources so that they are embedded in the assembly. On the Properties window, select Resource as the Build Action.