2009-05-29 2 views
1

私はあなたにコンバータを示しこんにちは、私は、データベース内の自分の画像を変換するコンバータを作成しようとしているデータ型「VARBINARY(マックス)」 WPFで私のデータグリッドを移入するが、私は2のエラーを持っている:コンバータバイナリからイメージWPF;

public class BinaryToImageConverter : IValueConverter 
{ 

public object Convert(object value, System.Type targetType, object parameter, 

System.Globalization.CultureInfo culture) 
    { 

    Binary binaryData = value;// here there is the first error .How convert BinaryData to Object?? 
     if (binaryData == null) { 
      return null; 
     } 

     byte[] buffer = binaryData.ToArray(); 
     if (buffer.Length == 0) { 
       return null; 
     } 

      BitmapImage res = new BitmapImage(); 
     res.BeginInit(); 
     res.StreamSource = new System.IO.MemoryStream(buffer); 
      res.EndInit(); 
     return res; 
     } 

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
     BitmapImage source = value;//How convert Bitmap to Object? 
      if (source == null) { 
       return null; 
      } 
      if ((source.StreamSource != null) && source.StreamSource.Length > 0) { 
      return GetBytesFromStream(source.StreamSource); 
     } 

     return null; 
     } 

    private Binary GetBytesFromStream(System.IO.Stream stream) 
    { 
      stream.Position = 0; 
     byte[] res = new byte[stream.Length + 1]; 
     using (System.IO.BinaryReader reader = new System.IO.BinaryReader(stream)) { 
       reader.Read(res, 0, (int)stream.Length); 
     } 
      return new Binary(res); 
    } 

} 

これは正しいか、これを行う良い方法がある場合は、アドバイスを私に教えてください? ご協力いただきありがとうございます。

Binary binaryData = (Binary)value; 

または

Binary binaryData = value as Binary; 

それが何をし、おそらく良いでしょうです:valueパラメータの型のオブジェクトが含まれている場合は が良い日に

+0

エラーの内容を示すことはできますか? – ChrisF

答えて

2

をお持ちのBinaryDataその後、あなたはそれを型キャストすることができますキャストする前に、キャストする前にvalueパラメータをチェックします。

+0

ありがとうTomLog、そうです:) – JayJay

関連する問題