views:

101

answers:

3

In SL class library MyLib, I have a image say my.png. Then I want to use it in code behind I tried following way:

StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("/MyLib;component/my.png", UriKind.Relative));    
BitmapImage image = new BitmapImage();                                
image.SetSource(resourceStream.Stream);
this.MyIcon.Source = image;   

But it's not woking. I think it's the Uri not set correctly. Help please.

A: 

Do you have your image marked as "Resource" in the properties window, or "Content"?

Jeff Wilcox
A: 

You could always set a style as a resource in your application and then call it like:

Application.Current.Resources["myCoolStyle"] and apply that to the image.

mstrickland
A: 

This works:-

BitmapImage image = new BitmapImage(new Uri("/MyLib;component/my.png", UriKind.Relative));
MyIcon.Source = image;

I can't see why you would want to use a Stream here. Having said that your Stream code should work. The build action on the png should be "Resource" and "MyLib" in your Uri should be the Assembly name of the library as found on the "Silverlight" tab of the project properties.

AnthonyWJones