Search code examples
c#wpfimageimagesource

ImageSource from String not working?


I have a bunch of *.tif images in a folder in my project..which i've also added to my visual studio project in a folder located "Templates\Team Logos"

now if i set an image source to say:

<Image Name="UL_Team1_Image" Grid.Row="1" Grid.Column="1" Margin="5" Source="Team Logos\ARI.tif"></Image>

That works. But now if i try:

UL_ImageArr[a].Source = (ImageSource)new ImageSourceConverter().ConvertFromString("Team Logos\\ARI.tif");

that doesn't work. What gives? I get a NullReferenceException... but it doesn't make sense to me?


Solution

  • You can use this in your code-behind, I think, it is more faster than using ImageSourceConverter.

    BitmapImage bimage = new BitmapImage();
    bimage.BeginInit();
    bimage.UriSource = new Uri("Team Logos\\ARI.tif", UriKind.Relative);
    bimage.EndInit();
    UL_ImageArr[a].Source = bimage;
    

    If you want to use ImageSourceConverter you must refer to image file by pack-uri:

    var converter = new ImageSourceConverter();
    UL_ImageArr[a].Source = 
        (ImageSource)converter.ConvertFromString("pack://application:,,,/Team Logos/ARI.tif");