Search
Serializing the Diagram

Saving and Loading Diagrams

Diagrams can be serialized in byte sequences wrapped by .NET streams. Such streams may represent files, memory buffers, communication pipes, TCP sockets, and so on. In serialized form a diagram might be saved in a database record too. To save the current diagram into a stream, call the SaveToStream method, and respectively to load a diagram from a stream call LoadFromStream. Two shortcut methods are provided for work with files. SaveToFile creates a disk file and a stream that wraps it and invokes SaveToStream. LoadFromFile opens a read-stream on a disk file and invokes LoadFromStream.

If using custom item classes, binary serialization for their additional properties can be implemented by overriding the SaveTo and LoadFrom methods. In addition, such classes must specify a string to use as their identifier in the stream by calling RegisterItemClass.

Saving Attached Data

All objects referenced by the diagram, such as images and fonts, are serialized together with the diagram elements. The Tag and Id objects that are associated with diagram elements are also saved if they are of value data type, if they are marked as [Serializable] or if their class implements the ISerializable interface. If a tag is not serializable it won't be saved by the Save* methods.

XML Serialization

It is possible to save a diagram as an XML document. To do that, call the SaveToXml method of the Diagram class. To deserialize a diagram back from its XML representation, call the LoadFromXml method.

Custom item classes can implement XML serialization for their additional properties by overriding the SaveToXml and LoadFromXml methods. In addition, such classes must specify a string to use as their identifier in the XML document by calling RegisterItemClass.

The control raises SerializeTag and DeserializeTag to enable serialization of complex Id and Tag objects without overriding the item classes. The PropertyName event argument specifies which property is being serialized.

JSON Serialization

To serialize a diagram as a JSON string, call its SaveToJson method. To deserialize a diagram back from its JSON representation, call the LoadFromJson method. The SaveToJsonFile and LoadFromJsonFile methods serialize the JSON strings to / from files.

Custom item classes can implement JSON serialization for their additional properties by overriding the SaveToJson and LoadFromJson methods. Such classes must be registered for serialization by calling the RegisterItemClass method and providing a string identifier for the clientClass parameter.

The control raises JsonSerializeTag and JsonDeserializeTag to enable serialization of complex Id and Tag objects without overriding the item classes. The PropertyName event argument specifies which property is being serialized.

JSON serialization is handled internally by the classes from MindFusion.Json namespace. The JsonValue class represents a property value. Property names and values are stored in instances of the JsonObject dictionary class. The JsonContext class handles the serialization of the Diagram properties. The Serialize method is a wrapper over the Serializer class' Serialize method. It takes a JsonObject and serializes it to a string. The Parse method is a wrapper over the Parser class' ReadJsonObject method. It takes a JSON-formatted string and returns a JsonObject with the parsed data.

Exporting Images

You can create a static image representing the current diagram using the CreateImage method. The method returns an instance of the System.Drawing.Bitmap class. The Bitmap class provides ways to save the image as a Jpeg, Gif or Bmp file, as shown in the following example:

C#  Copy Code

private void btnExport_Click(object sender, System.EventArgs e)
{
    Bitmap bmp = diagram.CreateImage();
    bmp.Save(@"E:\export.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

Visual Basic  Copy Code

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click

    Dim bmp As Bitmap = diagram.CreateImage()
    bmp.Save("E:\export.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

End Sub