Monday, October 29, 2012

how to add png for a image in code for silverlight?

how to add png for a image in code for silverlight?

Try to create a image in code as below:

sp = new StackPanel() { Orientation = Orientation.Horizontal }; Image myImage = new Image() { HorizontalAlignment = HorizontalAlignment.Left, Width = 16, Height = 16, Margin = new Thickness(1, 0, 0, 0) }; MyImage.Source = new BitmapImage(new Uri("/MyAssembly;component/folder/image1.png", UriKind.RelativeOrAbsolute)); sp.Children.Add(MyImage); 

then run the app, the image does not display. Check the data with Fiddler and got 404 error. The source for above image is something like:

http://localhost:80/MyAssembly;component/folder/image1.png 

but this image is complied into an assembly MyAssembly.

It is okay to get the image xaml from the assembly with following markup:

<Image Source="/MyAssembly;component/folder/image1.png"/> 

Confused. Not sure why. How to get dynamic image in code?

Answers & Comments...

Answer: 1

Don't know what scheme to use for that Uri in Silverlight, where you would write something like pack://application,,,/MyAssembly;component/folder/image1.png in WPF.

But fortunately you could always write this:

ImageSourceConverter converter = new ImageSourceConverter(); MyImage.Source = (ImageSource)converter.ConvertFromString(                      "/MyAssembly;component/folder/image1.png"); 
by : Clemenshttp://stackoverflow.com/users/1136211




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog