I have an Image in the view which is bound to a byte array in the view model.
<Image
Source="{Binding InputImage, Converter={StaticResource imageConvertor}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Panel.ZIndex="1"
x:Name="UploadImage"
Grid.Column="0"
Grid.Row="0"
Stretch="Uniform"
Margin="5"
/>
private byte[] inputImage;
public byte[] InputImage
{
get { return inputImage; }
set
{
inputImage = value;
OnPropertyChanged(nameof(InputImage));
}
}
The user loads an image via a button which executes this command
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true) { UploadImage.Source = new BitmapImage(new Uri(op.FileName)); }
The problems is that when I try to load an image the converter receives null as parameter. If I remove the converter from the binding in the xaml file it gets loaded successfully to the view but it cannot be bound to the property in the view model.
This is the implementation of the converter
public class ByteArrayToBitmapImageConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
else
return ToImage(value as byte[]);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
else
return ToBinary(value as Image);
}
public static Image ToImage(Byte[] binary)
{
Image image = null;
if (binary == null || binary.Length < 100) return image;
using (MemoryStream ms = new MemoryStream(binary))
{
image = Image.FromStream(ms);
}
return image;
}
public static Byte[] ToBinary(Image image)
{
if (image == null) return null;
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Png);
return memoryStream.ToArray();
}
}
}
What causes the null inputs?