Thursday, May 28, 2009

Tablet Programming Part 2: Save, Load

This part builds off Tablet Programming Part 1, if you need to follow along then I advise completing it first.
In this step we will learn how to save and load ink.
Let's start with save. There are many file types that you can save to, but for these purposes I will show you the simplest and most useful, .ISF. With an .ISF file you can open it back up and continue adding strokes, wheras other formats are primarily for displaying a finished result such as a .GIF or .JPG file.

You won't need to add another reference, a simple imports will do.

Imports System.IO

You will also need to add an OpenFileDialog and a SaveFileDialog control to your form.

Drag another button to the bottom section of the split container, this will be the save button, place the following code in the event handler:

Dim inkBytes As Byte() = Me.inkOverlay.Ink.Save()
Me.SaveFileDialog1.FileName = "NewInk.isf"
Me.SaveFileDialog1.DefaultExt = ".isf"
If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim strm As FileStream = File.Open(Me.SaveFileDialog1.FileName, FileMode.Create)
strm.Write(inkBytes, 0, inkBytes.Length)
strm.Close()
End If

The first line is just prepping the data to be saved via file stream, the next two are formatting the save dialog to default to a particular name and file type. The next part just ensures that they hit 'OK' and actually want to save, and then you decide how you will handle the creation of the file with FileMode.Create. The rest is just the streamwriter creating the file, its really pretty simple and straightforward.

Loading is just as simple except a little added work for the end users sake, throw another button on your form to handle the loading.

If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If Me.OpenFileDialog1.CheckFileExists = True Then
strm = File.OpenRead(Me.OpenFileDialog1.FileName)
Dim info As FileInfo = New FileInfo(Me.OpenFileDialog1.FileName)
Dim inkBytes(info.Length) As Byte
strm.Read(inkBytes, 0, info.Length)
strm.Close()
Dim newInk As New Microsoft.Ink.Ink
newInk.Load(inkBytes)
Me.inkOverlay.Enabled = False
Me.inkOverlay.Ink = newInk
Me.inkOverlay.Enabled = True
End If
End If

This time we are doing a little more formatting by filtering all files that aren't .isf (though we give a selection to view all files). Next, instead of building a function to test whether or not a path is valid, we use .NETs own ability to do this for us. If they choose 'OK' and the filepath is valid then the loading occurs in the same way we saved, just in reverse. Once the load has occured, we must disable the ink object, set it to the loaded ink, and then re-enable. That's it for the load. As mentioned, using .ISF allows the recognizer to utilize the strokes from the saved files, you can test this by performing the load and then making a stroke, you will see that the textbox reads all of the loaded strokes.

Part 3: Pen Input is next.

No comments: