user695663
user695663

Reputation: 12356

Setting a source property of Image in Silverlight using converter

I have a page where I want to display images in a control Template. For this I only get the Image name to show. So I am using a converter to return BitmapImage like this:

return new BitmapImage(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));   

in the XAML binding i have:

Image Source="{Binding ThumbNail,Converter={StaticResource MapImagePath}}"/>

For some reason i am not able to see the image at all.

I have changed my image extension to .jpeg but still not working.

Am i doing some thing wrong or suggest me if I am wrong. Thanks.

Upvotes: 0

Views: 808

Answers (3)

jee
jee

Reputation: 524

  1. First change your image property to content.[Right click to your image, go to property and change build action from Resource to content

  2. Now suppose we have to "/Resources/Images/1.png" file.

Write code something like below in your converter.

public object Convert(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
{
    BitmapImage bitmap = new BitmapImage();
    Uri uri = new Uri(@"Resources/Images/1.png", UriKind.Relative);
    bitmap .SetSource(Application.GetResourceStream(uri).Stream);
    return bitmap ;
}

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189457

Modify your converter so that it returns an instance of BitmapImage. Your converter is currently returning a string or a Uri however the actual type for the Source property is ImageSource.

The conversion of a string to a BitmapImage is some magic that the XamlParser does for us but when we supply the value using a Converter we need to give it the correct type.

Upvotes: 1

Nilesh Gule
Nilesh Gule

Reputation: 1611

You can try using ImageFailed event to check if there is any exception being thrown while loading the image. Usually you get AG_E_NETWORK_ERROR if there is any network problem while loading images from internet.

I had faced this problem while running the Silverlight application from file system which is default in Visual Studio 2010 using the built in web server. I moved the hosting web application to local IIS server and then the images were visible.

Upvotes: 0

Related Questions