2014-11-03 59 views
0

我有如下勢必圖像:在image.source不圖使用轉換器

<Image x:Name="ImgFoto" Source="{Binding Path=Foto, 
         Converter={StaticResource imageConverter}, Mode=TwoWay, 
         UpdateSourceTrigger=PropertyChanged}" Stretch="Fill" 
         HorizontalAlignment="Left" 
         Height="148" Margin="25,23,0,0" VerticalAlignment="Top" 
         Width="193"/>" 

「圖片」是Image類型的SQL領域

我的轉換器類:

public sealed class ImageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
          object parameter, CultureInfo culture) 
    { 
     try 
     { 
      return new BitmapImage(new Uri((string)value)); 
     } 
     catch 
     { 
      return new BitmapImage(); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, 
           object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我的問題是,當你運行該應用程序的圖像顯示爲空白。

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2014-11-03 01:30:48

+0

這裏很難說出什麼問題。嘗試查看*** Output ***窗口,看看是否有任何錯誤通知。順便說一句,我認爲你不需要任何自定義轉換器在這裏。默認的Converter可以將URI字符串轉換爲一些ImageSource。 – 2014-11-03 01:52:06

回答

0

「圖片」的類型是圖片

的SQL場如果它是類型圖像的領域,那麼它以二進制格式存儲和轉換器,你將其轉換爲字符串。這將永遠不會工作,因爲它是二進制格式。您需要獲取字節並將其轉換爲位圖,然後將其返回。

另外,如果您不打算更改圖像,請將其綁定OneWay

1

SQL圖像類型是二進制的,所以你必須將二進制轉換爲bitmapimage而不是uri soirce。試試下面轉換爲

public sealed class ImageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
          object parameter, CultureInfo culture) 
    { 
     if (value == null || value.Length == 0) return null; 
     var image = new BitmapImage(); 
     using (var mem = new MemoryStream((byte[])value)) 
     { 
      mem.Position = 0; 
      image.BeginInit(); 
      image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; 
      image.CacheOption = BitmapCacheOption.OnLoad; 
      image.UriSource = null; 
      image.StreamSource = mem; 
      image.EndInit(); 
     } 
     image.Freeze(); 
     return image; 
    } 

    public object ConvertBack(object value, Type targetType, 
           object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

你好。在表單上我有一個上傳圖片的按鈕,然後記錄下來。我的意思是,我需要一個轉換器,並使用TwoWay。我會嘗試你推薦的和你評論的。非常感謝你。 – 2014-11-03 12:11:30

+0

你好。我做了你提供給我的工作。 但我現在真正的問題是,CARGO記錄中的圖像時,所有其他記錄中都會顯示相同的圖像。 在您通過Listview查看記錄時加載並保存圖像後,Image.Source對每個人都是一樣的。 我必須退出並重新進入正確刷新Image.Source的應用程序。 – 2014-11-03 19:52:07

+0

@MarioEscudero聽起來像你想在你的圖像源屬性上實現INotifyPropertyChanged。 – kenny 2014-11-03 21:28:02

0

我的新XAML是:

  <Image x:Name="ImgFoto" Source="{Binding Foto}" Stretch="Fill" HorizontalAlignment="Left" 
         Height="148" Margin="25,23,0,0" VerticalAlignment="Top" Width="193"/> 

我對荷圖像代碼:

private void BtnCargarFoto_Click(object sender, RoutedEventArgs e) 
    { 
     OpenFileDialog OD = new OpenFileDialog(); 
     OD.Filter = "jpg(*.jpg)|*.jpg|png(*.png)|*.png|gif(*.gif)|*.gif|bmp(*.bmp)|*.bmp|All Files(*.*)|*.*"; 
     if (OD.ShowDialog() == true) 
     { 
      using (Stream stream = OD.OpenFile()) 
      { 
       bitCoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); 
       ImgFoto.Source = bitCoder.Frames[0]; 
      } 
      System.IO.FileStream fs = new System.IO.FileStream(OD.FileName, System.IO.FileMode.Open); 
      foto = new byte[Convert.ToInt32(fs.Length.ToString())]; 
      fs.Read(foto, 0, foto.Length); 
     } 
    } 

問題:

綁定非常好。 現在的問題是,在記錄中對圖像進行充電時,所有其他記錄中都會顯示相同的圖像。 在您通過Listview查看記錄時加載並保存圖像後,Image.Source對每個人都是一樣的。 我必須退出並重新輸入Image.Source的應用程序才能正常刷新。 非常感謝。