2017-09-15 109 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}}"/>