KentZhou
KentZhou

Reputation: 25553

How to set uri for local image in silverlight app?

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.

Upvotes: 2

Views: 1428

Answers (3)

AnthonyWJones
AnthonyWJones

Reputation: 189457

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.

Upvotes: 1

strickland
strickland

Reputation: 1993

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.

Upvotes: 0

Jeff Wilcox
Jeff Wilcox

Reputation: 6385

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

Upvotes: 0

Related Questions