views:

26

answers:

2

I've got this definition in my XAML:

<Image Name="AlbumArt" Source="/AlbumChooser2;component/Resources/help.png" />

The image is display OK on startup.

In my code I'm looking for mp3's to play and I display the associated album art in this Image. Now if there's no associated image I want to display a "no image" image. So I've got one defined and I load it using:

BitmapImage noImage = new BitmapImage(
              new Uri("/AlbumChooser2;component/Resources/no_image.png",
                      UriKind.Relative));

I've got a helper class that finds the image if there is one (returning it as a BitmapImage), or returns null if there isn't one:

if (findImage.Image != null)
{
    this.AlbumArt.Source = findImage.Image; // This works
}
else
{
    this.AlbumArt.Source = noImage; // This doesn't work
}

In the case where an image is found the source is updated and the album art gets displayed. In the case where an image isn't found I don't get anything displayed - just a blank.

I don't think that it's the setting of AlbumArt.Source that's wrong, but the loading of the BitmapImage.

If I use a different image it works (e.g. the original help image), but even if I recreate the "no image" image it doesn't work. What could be wrong?

+1  A: 

Some ideas to think about/try:

What are the Visual Studio settings for no_image.png? Is it an "Embedded Resource" or "Content"? Do you have it set to "Copy Always" or "Copy If Newer"?

It looks like your referencing the image as a resource, but have you actually added it to your project's resources? If it is a resource, I believe you should set it the properties to "Resource" and "Copy Never".

What happens if you make your UriKind UriKind.Absolute and set the path to the actual file on disk?

If you look at noImage in the debugger at the point you're setting it to the Source, is it definitely non-null and does it have the correct width/height?

DanM
The image has been loaded and has the right height and width. The build action was "None" - setting it to "Resource" fixed the problem.
ChrisF
A: 

try this

http://stackoverflow.com/questions/2748930/binding-bitmapimge-to-image-in-wpf

when wpf compile to .exe resource directory became part of your .exe and u cant access that from this code

BitmapImage noImage = new BitmapImage(
          new Uri("/AlbumChooser2;component/Resources/no_image.png",
                  UriKind.Relative))

so You must use this code Resources.no_image.png and convet that to BitmapImage.

Regards REV

Rev