That’s my basic code:
public class MyObject
{
public decimal MyDeciaml { get; set; }
public string? MyImageBase64
{
get
{
if (MyImageBase64 != null)
{
byte[] imageBytes = Convert.FromBase64String(MyImageBase64);
using (MemoryStream stream = new MemoryStream(imageBytes))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
MyImageBitmap = bitmapImage;
}
}
return MyImageBase64;
}
set { }
}
[JsonIgnore]
public BitmapImage? MyImageBitmap { get; set; }
}
which I bind using a ViewModelData
class and ObservableCollection<MyObject> myObjects = new ObservableCollection<MyObject>();
against a ListBox:
<ListBox ItemsSource="{Binding MyObjects}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<Image x:Name="myImage"
Source="{Binding MyImageBitmap}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Width="Auto"
Height="Auto" />
</Grid>
<TextBlock Text="{Binding MyDeciaml }" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
It seems to works nice on binding. Now, when I serialize it on json using Newtonsoft.Json
‘s JsonConvert.SerializeObject(data)
, I see this on the json:
"MyImageBitmap": "System.Windows.Media.Imaging.BitmapImage"
How can I remove it? It seems [JsonIgnore]
has no effect?
I want to remove, and just keep MyImageBase64 (on deserialization, convert it to BitmapImage
, and get back the image).
Thanks for any tips on this. Maybe a better way is to bind directly the base64 string and do the conversion to BitmapImage on ListBox, if possible?
2
Answers
Your code won’t work at all. An assignment to the
MyImageBase64
property has no effect because the property setter is empty. And the getter would recursively call itself and hence end up in a StackOverflow exception.You may write it like shown below, where the BitmapImage is only created when the getter of the (read-only)
MyImageBitmap
property is actually accessed.Besides that, it is unclear why the
JsonIgnore
attribute won’t work in your case. You may however omit theMyImageBitmap
completely and create the BitmapImage in a Binding Converter.The converter implementation could like like this:
In XAML, it would typically be declared as resource:
And used in a Binding like this:
Use a byte array for the image in the data class.
Image.Source
can be bound to a byte arraySo change your class to
and the Xaml to