tags:

views:

561

answers:

1

I have the following XAML that displays a cover image for a book using a URI:

<Rectangle.Fill>
    <ImageBrush ImageSource="{Binding CoverUrl}" />
</Rectangle.Fill>

However, the image I would like to use is not on disk anywhere or accessible via a URI; it comes from inside a binary file that I parse out into a BitmapImage object.

When I create a BitmapImage object via code, the resulting object's BaseUri and UriSource properties are null. How can I get the ImageBrush to use a BitmapImage that resides in memory instead of reading it in from a URI?

+1  A: 

The ImageSource property is of type ImageSource, not Uri or string... actually, a conversion happens when you assign a Uri to it. You can bind the ImageBrush directly to a property that returns an ImageSource

<Rectangle.Fill>
    <ImageBrush ImageSource="{Binding Cover}" />
</Rectangle.Fill>


private ImageSource _cover;
public ImageSource Cover
{
    get
    {
        if (_cover == null)
        {
            _cover = LoadImage();
        }
        return _cover;
    }
}
Thomas Levesque
Thanks, I knew it was something silly and easy.
Matt Bridges