2012-07-17 5 views
1

MVVM을 사용하고 있는데 ViewModel에 BitmapData 컬렉션이 있습니다. 데이터 바인딩을 통해 이미지로보기로 표시하려고합니다.BitmapData의 데이터를 WPF Image Control에 바인딩하는 방법?

어떻게하면됩니까?


솔루션 :

[ValueConversion(typeof(BitmapData), typeof(ImageSource))] 
public class BitmapDataConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     BitmapData data = (BitmapData)value; 
     WriteableBitmap bmp = new WriteableBitmap(
      data.Width, data.Height, 
      96, 96, 
      PixelFormats.Bgr24, 
      null); 
     int len = data.Height * data.Stride; 
     bmp.WritePixels(new System.Windows.Int32Rect(0, 0, data.Width, data.Height), data.Scan0, len, data.Stride, 0, 0); 
     return bmp; 
    } 

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

답변

0

해결책, Clemens에게 감사드립니다.

[ValueConversion(typeof(BitmapData), typeof(ImageSource))] 
public class BitmapDataConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     BitmapData data = (BitmapData)value; 
     WriteableBitmap bmp = new WriteableBitmap(
      data.Width, data.Height, 
      96, 96, 
      PixelFormats.Bgr24, 
      null); 
     int len = data.Height * data.Stride; 
     bmp.WritePixels(new System.Windows.Int32Rect(0, 0, data.Width, data.Height), data.Scan0, len, data.Stride, 0, 0); 
     return bmp; 
    } 

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

당신이 경로 (ImageSourceConverter에서 제공) 자동 변환의 존재에 곰 이미지 파일에서 Image.Source을 설정할 수 있다는 사실.

Image.Source를 BitmapData 유형의 객체에 바인딩하려는 경우 다음과 같이 binding converter을 작성해야합니다. BitmapData에서 WritableBitmap을 작성하는 방법에 대해 자세히 알아야합니다.

[ValueConversion(typeof(System.Drawing.Imaging.BitmapData), typeof(ImageSource))] 
public class BitmapDataConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     System.Drawing.Imaging.BitmapData data = (System.Drawing.Imaging.BitmapData)value; 
     WriteableBitmap bitmap = new WriteableBitmap(data.Width, data.Height, ...); 
     bitmap.WritePixels(...); 
     return bitmap; 
    } 

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

아마도 this question이 변환을 구현하는 데 도움이됩니다.