How to bind data from BitmapData to WPF Image Control?

丶灬走出姿态 提交于 2019-12-24 10:45:35

问题


I'm using MVVM and in my ViewModel I've got some BitmapData collections. I want them to appear in my View as Images through data binding.

How can I do that?


Solution:

[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();
    }
}

回答1:


The fact that you can set Image.Source from an image file path bears on the existence of an automatic conversion (provided by ImageSourceConverter).

In case you want to bind Image.Source to an object of type BitmapData you would have to write a binding converter that could look like below. You would however have to find out about the details of writing a WritableBitmap from BitmapData.

[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();
    }
}

Maybe this question is helpful for implementing the conversion.




回答2:


Solution, thanks to 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();
    }
}


来源:https://stackoverflow.com/questions/11518625/how-to-bind-data-from-bitmapdata-to-wpf-image-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!