How do I format a Uri when binding an Image in Silverlight?

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2009-08-05T01:41:45Z Indexed on 2010/05/13 16:14 UTC
Read the original article Hit count: 209

Filed under:
|
|
|
|

I haven't been able to find an answer to this.

I have a database that has image paths in it ("images/myimage.jpg"). These images exist on my asp.net site which is also where I host the SL. I want to bind these images to my ListBox control so that the image displays.

I have read that since I have a string value, "images/myimage.jpg", that I need to convert it to a BitMap image. I have done this:

the xaml:
 <Image Source="{Binding ImageFile, Converter={StaticResource ImageConverter}}"/>


the ImageConverter class:
    public object Convert(object value, Type targetType,
                                  object parameter, CultureInfo culture)
            {
                try
                {
                    Uri source= new Uri(value.ToString());
                    return new BitmapImage(source);
                }
                catch(Exception ex)
                {
                    return new BitmapImage();
                }
            }

I get an error when creating the Uri, "The Format of the URI could not be determined". What am I doing wrong? If I create a Uri that looks like this: http://localhost:49723/images/myimage.jpg, it works just fine.

Why doesn't just "images/myimage.jpg" work?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about data