2017-09-15 38 views
0

我有一个固定的文档,其中有一个图像。图像的source-property绑定到文档的datacontext中的字节数组(从数据库读取)。当我将鼠标移动到图像上时,我得到一个filenotfoundexception。wpf documentviewer在鼠标悬停的图像上抛出异常

它看起来像文档查看器试图加载有关工作目录中当前不存在的名为“图像”的文件中的渲染图像的附加信息。

有人知道如何禁用此行为吗?

+0

我们需要了解您的问题更多信息,最重要的是如何将图像分配给控件。我通常使用转换器并将字节数组转换为位图并将其返回给图像控件。 – XAMlMAX

+0

当前图像的源属性被直接绑定到字节数组: \t \t \t <图像保证金= “330 80 0 0” 宽度= “132.2832” 高度= “170.0784” 源=” {Binding PassPhoto}“/> public byte [] PassPhoto { get {return this.person.PassPhoto; }} \t \t \t \t 我会尽量解决方案... – Hurby

+0

尝试使用转换器和变换的字节数组为位图。 [更多信息](https://stackoverflow.com/a/21555447/2029607) – XAMlMAX

回答

0

您可以创建一个字节数组的BitmapImage具有以下转换器:

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; 
    } 
} 

然后在你的XAML可以使用此像这样:

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