2012-03-30 1 views
0

나는 얼마 동안이 문제를 다루었 다. 내 DB에서 이미지를 바이트 []로 가져오고 WritableBitmap으로 변환하여 바인딩을 사용하여 내 xaml 페이지에 표시 할 수 있습니다.바이트 어레이에서 WriteableBitmap 이미지 WPal7에 대한 IValueConverter

나는 이것을 사용하고 있습니다 :

public class ImageConverter : IValueConverter 
{ 
    /// <summary> 
    /// Converts a Jpeg byte array into a WriteableBitmap 
    /// </summary> 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is byte[]) 
     { 
      MemoryStream stream = new MemoryStream((Byte[])value); 
      WriteableBitmap bmp = new WriteableBitmap(200, 200); 
      bmp.LoadJpeg(stream); 
      return bmp; 
     } 
     else 
      return null; 
    } 
    /// <summary> 
    /// Converts a WriteableBitmap into a Jpeg byte array. 
    /// </summary> 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

첫 번째 문제는 그것이 작동하지 않는다는 것입니다. 안타 때 예외가 throw됩니다. bmp.LoadJpeg(stream);

두 번째 문제는 WriteableBitmap 생성자에 전달되는 고정 크기에 관한 것이며, db에서 오는 사진의 크기를 어떻게 알 수 있습니까? 어떻게 든 동적으로 만들 수 있습니까? 나는 두 번째 문제가 첫 번째 문제의 원인이라고 생각한다.

감사합니다.

편집

나는 또한이 같은 PictureDecoder.DecodeJpeg()를 사용하여 시도합니다 한

:

  MemoryStream stream = new MemoryStream((Byte[])value); 
      WriteableBitmap bmp = PictureDecoder.DecodeJpeg(stream); 
      return bmp; 

하지만 중 하나가 작동하지 않았다. 이 경우 PictureDecoder.DecodeJpeg 나를 위해 bmp 개체를 만드는 것으로 가정합니다. 나는 여전히 불특정 오류가 발생합니다. 스트림에 허용되는 최대 길이를 넘을 수 있습니까?

답변

1

덕분에이 문제가 DB에서 나오는 스트림은 어떻게 든 손상된이었다 것으로 보인다. 가치 변환기는 실제로 괜찮 았어. 이

public class ImageConverter : IValueConverter 
{ 
/// <summary> 
/// Converts a Jpeg byte array into a WriteableBitmap 
/// </summary> 
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    if (value is byte[]) 
    { 
     MemoryStream stream = new MemoryStream((Byte[])value); 
     WriteableBitmap bmp = PictureDecoder.DecodeJpeg(stream); 
     return bmp; 
    } 
    else 
     return null; 
} 
/// <summary> 
/// Converts a WriteableBitmap into a Jpeg byte array. 
/// </summary> 
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    throw new NotImplementedException(); 
} 
} 
+0

파일의 원래 알파 데이터는 포함되지 않습니다. –

2

저는 이것을 사용하지만 BitmapImage을 반환합니다. WriteableBitmap가 반환 되나요?

편집 : 당신이 반환해야 할 경우 Ritch이 코멘트에 언급 한 바와 같이 WriteableBitmap는 두 번째 문제는 내가 크기를 알 수있는 방법은 WriteableBitmap 생성자에 전달 된 고정 된 크기에 관한 것입니다

var writeableBitmap = new WriteableBitmap(bitmapImage); 
return writeableBitmap 

를 추가 사진에서 은 DB에서 오는 것입니까?

BitmapImage가 생성되면 bitmapImage.PixelWidthbitmapImage.PixelHeight에 액세스 할 수 있습니다. 답변

에 대한

public class ByteArraytoImageConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (value == null) return null; 

      var byteBlob = value as byte[]; 
      var ms = new MemoryStream(byteBlob); 
      var bmi = new BitmapImage(); 
      bmi.SetSource(ms); 
      return bmi; 
     } 

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

그냥'var에 WB = 새로운 WriteableBitmap (BMI)를 추가 더 깨끗하고 동적 인 것, 그래서 내가 대신 PictureDecoder.DecodeJpeg()를 사용하도록 변경되었습니다; WriteableBitmap을 반환하는 경우에는이 문제에 실제로 필요하지는 않지만 보이지는 않습니다. –

+0

좋은 지적, 나는 그것을 추가 할 것이다. –