wpf documentviewer throws exception on mouse over image

241 views Asked by At

I have a fixeddocument in which i have an image. The source-property of the image is bound to a byte-array (read from the database) in the datacontext of the document. When I am moving the mouse over the image I get a filenotfoundexception.

It looks like the documentviewer tries to load additional information about the rendered image from a file named "image" in the working directory which of course does not exist.

Does somebody know how to disable this behavior?

1

There are 1 answers

0
XAMlMAX On

You can create a BitmapImage from a byte array with the following converter:

public class BytesToBitmapConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var bytes = (byte[])value; // make sure it is an array beforehand

        using (var ms = new System.IO.MemoryStream(bytes))
        {
            var image = new BitmapImage();
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.StreamSource = ms;
            image.EndInit();
            return image;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}  

Then in your xaml you would use this like so:

<Image Source="{Binding propertyNameHere, Converter={StaticResource converterName}}"/>