Search
Serialization of NetDiagram Diagrams

Saving and Loading Diagrams

NetDiagram 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 saving and loading of 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.

Saving Custom Data

All objects referenced by the diagram, such as images and fonts, are serialized together with the diagram elements. The Tag objects 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.

If a type used to store custom data is renamed or moved to a different assembly or namespace, files where its instances are stored can be loaded by implementing a custom SerializationBinder and assigning it to the SerializationBinder property.

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.

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);
}