Saving Ink as a Byte Array
Microsoft has made it so that the Ink class knows how to save its own data into a byte array. When you save Ink, you are saving bytes into a file format called Ink Serialization Format. This file format is proprietary to Microsoft. As shown in Listing 5, we get the byte array that represents an Ink by calling the Ink.Save() method. Once we get the Ink’s bytes, the application’s custom Save() method is called, passing in the Ink’s bytes as a parameter. Within the Save() method, a dialog box is opened, a file is named, and the bytes are saved to that file.
Listing 5 Save menu event handler and the Save() method
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { //Save the Ink’s byes by calling Ink.Save(); byte[] bytes = inkCollector.Ink.Save(PersistenceFormat.InkSerializedFormat); this.Save(bytes); } private void Save(byte[] bytes) { //Create a dialog box SaveFileDialog dlgSave = new SaveFileDialog(); //Filter for the Ink Serialization Format dlgSave.Filter = "ISF files (*.isf)|*.isf| All files (*.*)|*.*"; if (dlgSave.ShowDialog() == DialogResult.OK) { //Create a stream using the entered file name Stream stream = dlgSave.OpenFile(); if (stream != null) { //Write the bytes to the stream stream.Write(bytes, 0, bytes.Length); //close the file stream.Close(); //Set the private member variable that indicates //file name in play for the app. currentFileName = dlgSave.FileName; } } }
When you look in the SDK documentation, you’ll see that the Ink.Save() method has three overloads:
- The following saves the Ink as a byte array using the defaults:
byte[] Ink.Save()
- The following saves the Ink as a byte array according to the
PersistenceFormat value: Base64Gif, Base64InkSerializedFormat, Gif,
InkSerializedFormat:
byte[] Ink.Save(PersistenceFormat)
- The following saves the Ink as a byte array according to the
PersistenceFormat value, using the specified compression format: Default,
Maximum, NoCompression:
byte[] Ink.Save(PersistenceFormat, CompressionMode)
This article uses theInk.Save(PersistenceFormat) method, which requires that we define which Persistence format we want to use. Let’s take a look at PersistenceFormat.
As mentioned above, you can save Ink in one of four formats: Base64Gif, Base64InkSerializedFormat, Gif, InkSerializedFormat.
Base64Gif
Saving Ink using a PersistenceFormat of the Base64Gif results in the data being saved to a Gif image, as described below. However, saving to Base64Gif saves the Gif as a Base64Encoded string.
Base64InkSerializedFormat
The Base64InkSerializedFormat saves Ink in Ink Serialized Format. However instead of saving it as binary data, it is saved to a Base64encoded string.
Gif
The Gif format enables you to save the Ink data as a Gif image. This is useful if you need to display the Ink on a device that is not Ink-enabled. When you save to Gif format, the Ink information is stored as metadata within the graphic file format, which enables you to do Ink manipulation on devices that are Ink-enabled while rendering on any device, Ink-enabled or not.
InkSerializedFormat
InkSerializedFormat, which is the format that is most compact, enables you to store and work with Ink as binary data. Thus, you can save the Ink to the clipboard and store it in a database as a binary data type.