views:

20

answers:

2

I have a problem with a image when trying to display it. In my project i have a Class witch have a "public String Image" atribute. I have a web server localy wich return me a colection of Class. When i look in debug mode at the Image atribute it show me the corect url (if i paste the url in browser it show me the image) but the image isn't display. If instead i put any url from an image from internet it show me the image. I don`t understand why the image from local server isn't shown in silverlight app, but in browser it is. The code used in Silverlight is:

<Image Name="photoImage" Source="{Binding Image}" Margin="30,10,30,10" />

Thanks.

A: 

You need to bind to an image source, not a string For instance:

Uri Uri = new Uri(imageUrl, UriKind.Relative);
return new BitmapImage(Uri);
vc 74
+1  A: 

Try using this converter:

public class RelativeImageSourceConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) { return null; }
        var originalString = value.ToString();
        if (!Uri.IsWellFormedUriString(originalString, UriKind.RelativeOrAbsolute)) { return null; }
        var imageUri = new Uri(originalString, UriKind.RelativeOrAbsolute);
        if (!imageUri.IsAbsoluteUri)
        { 
            var hostUri = Application.Current.Host.Source;
            imageUri = new Uri(hostUri, originalString);
        }
        var image = new BitmapImage(imageUri);
        return image;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Murven
Thanks, with a little modification in another place this worked.
tribanp