Serialization in .NET MAUI Image Editor
18 Nov 20183 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);
}
}
}
Serialization and deserialization are not applicable for custom annotation views.
The Deserialize method provides an optional boolean parameter that determines whether the deserialization operation is added to the undo/redo history. By default, the value is true. When set to false, the deserialized annotations are loaded into the Image Editor but are excluded from the undo/redo history. As a result, Undo, Redo, and Reset operations affect only the changes performed after deserialization, while the deserialized annotations remain unchanged.
Due to limitations in Ahead-of-Time (AOT) compilation, reflection-based operations like serialization and deserialization are not supported in AOT-compiled environments.