How To Convert Ink To A Byte String

You may want to store ink for later retrieval or maybe even transfer ink between distributed applications. This can be done by serializing the ink into a byte array.

Serialization: Convert Ink to a Byte String

Start with an existing C# application that already has an Ink Collector object setup.

First you need to include a text object that contains needed objects and methods for serialization:

using System.Text;

Then use the following code to extract ink from the Ink Collector and convert it to a byte array or string.

// This object will encode our byte data to a UTF8 string
UTF8Encoding utf8 = new UTF8Encoding();

byte[] base64ISF_bytes;
string base64ISF_string;

// Get the base64 encoded ISF
base64ISF_bytes = myInkCollector.Ink.Save(PersistenceFormat.Base64InkSerializedFormat);

// Convert it to a String
base64ISF_string = utf8.GetString(base64ISF_bytes);

// get the string without the null at the end
string byteString = base64ISF_string.Substring(0,base64ISF_string.Length-1);

Deserialization: Convert a Byte String to Ink

Use the following code to convert a byte array back to ink and place it in an Ink Collector.

// this converts a base 64 string to a byte array and loads it into the ink collector

string byteString = "your string of bytes representing ink";
UTF8Encoding utf8 = new UTF8Encoding();  

// place ink in a temporary ink container because you can't load into a collector that has already been inked
Ink loadedInk = new Ink();

// clear the contents of the ink collector

bool inkCollectorEnabled = myInkCollector.Enabled;
// copy the temporary ink container into the ink collector
// this requires disabling the ink collector first
myInkCollector.Enabled = false;
myInkCollector.Ink = loadedInk;
myInkCollector.Enabled = inkCollectorEnabled;

Note: You can only load ink into an empty Ink Collector object that does not already have ink. This is why Ink.DeleteStrokes is called before loading ink.