Serialization in .NET MAUI Image Editor (SfImageEditor)
21 Jul 20263 minutes to read
The Image Editor control supports serializing and deserializing the shape, text, and pen annotations along with their settings. You can save the current state of the annotations and load it back when needed.
Serialization
The Serialize method serializes the current edits of annotations. Pass a stream as a parameter to store the SfImageEditor annotations in the stream.
<Grid RowDefinitions="0.9*, 0.1*">
<imageEditor:SfImageEditor x:Name="imageEditor"
Source="image.jpeg" />
<Button Grid.Row="1"
Text="Serialize"
Clicked="OnSerializeClicked" />
</Grid>using System.IO;
using Microsoft.Maui.Storage;
using Syncfusion.Maui.ImageEditor;
private void OnSerializeClicked(object sender, EventArgs e)
{
string filePath = Path.Combine(FileSystem.Current.CacheDirectory, "ImageEditor.xml");
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
this.imageEditor.Serialize(fileStream);
}
}Deserialization
The Deserialize method deserialize the annotations over an image. It reloads the SfImageEditor control with the annotations stored in the stream.
<Grid RowDefinitions="0.9*, 0.1*">
<imageEditor:SfImageEditor x:Name="imageEditor"
Source="image.jpeg" />
<Button Grid.Row="1"
Text="Deserialize"
Clicked="OnDeserializeClicked" />
</Grid>using System.IO;
using Microsoft.Maui.Storage;
using Syncfusion.Maui.ImageEditor;
private async void OnDeserializeClicked(object sender, EventArgs e)
{
var result = await FilePicker.PickAsync();
if (result != null)
{
using (Stream stream = await result.OpenReadAsync())
{
this.imageEditor.Deserialize(stream);
}
}
}
NOTE
Serialization and deserialization are not applicable for custom annotation views.
NOTE
Due to limitations in Ahead-of-Time (AOT) compilation, reflection-based operations like serialization and deserialization are not supported in AOT-compiled environments.