tags:

views:

182

answers:

2

Hi,

In my silverlight assembly I have several images as Resource. To access them I use the following syntax:

/ASSEMBLY_NAME;component/PATH_TO_MY_IMAGE.png

I'm creating BitmapImages from codebehind and I would like to be able to know in runtime if a given image path exists as a Resource on the assembly so that I can decide If I use that image or a default one (which I know will always exist).

Is there any way to achieve this? - Reflection? - Try to instantiate the BitmapImage and check for any error?

Many thanks, Bruno

+1  A: 
Application.GetResourceStream(new Uri("/ASSEMBLY_NAME;component/PATH_TO_MY_IMAGE.png"));

This will return a StreamResourceInfo if the file exists and null if it doesn't.

Or maybe the 'Stream' on the StreamResourceInfo is null if the resource doesn't exist - I wrote code to do this but it was a while ago so I don't remember exactly. It wasn't hard though, so I doubt you'll have problems figuring it out.

Alun Harford
A: 

Maybe its not something thats needed in silverlight but dont forget the pack://application:,,,

Application.GetResourceStream(new Uri("pack://application:,,," + "/ASSEMBLY_NAME;component/PATH_TO_MY_IMAGE.png"));

And if the resource does not exist it fires an IOException

Phil Jory