Question

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?

Was it helpful?

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");

OTHER TIPS

In code behind you will usually write the full pack URI to reference image resources.

So you either write

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

or

UL_ImageArr[a].Source = new BitmapImage(new Uri(
                        "pack://application:,,,/Team Logos/ARI.tif"));

or

UL_ImageArr[a].Source = BitmapFrame.Create(new Uri(
                        "pack://application:,,,/Team Logos/ARI.tif"));

If you are on WinRt, use this

string url = "ms-appx:///Assets/placeHolder.png";
return new BitmapImage(new Uri(url));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top