Advertisment

Developing for the Tablet PC — Part 3.

author-image
CIOL Bureau
Updated On
New Update

We saw in the last episode how simple it is to develop ink aware applications and also how the forms in Visual Studio .NET 2005 have adapted well to the creation of application that can capture ink without any additional efforts.

Advertisment

Today we will dive deeper into developing for the tablet PC and see how most of the features that have been developed for the client-server environment have been adopted well to suit the Tablet PC environment. Let us see a simple case on how to preserve the ink that is captured in the places where the signature is entered so that this can be reloaded and verified like how verification is done by the law or verification agencies.

Before we start with the example and coding let us see how the features that the Tablet PC SDK has provided for saving the ink that is entered in the applications. In addition to the controls that we saw in the first part of the series Microsoft has also provided the "InkOverlay" class which represents an object that is useful for annotation scenarios where users are not concerned with performing recognition on ink but instead are interested in the size, shape, color, and position of the ink. That means that no emphasis on the recognition of the ink will be done but the ink will be captures as it is.

An InkOverlay object creates an Ink object by default and this ink object has the method Save which is used to save the ink in persistent format. Let us see the various methods in which the ink can be saved. By default, the Save method will output the data in the Ink Serialized Format (ISF), which is the Tablet PC Platform's native binary format for ink data. The syntax to save the ink is.

Advertisment

Ink.Save(PersistenceFormat p)



Ink.Save(PersistenceFormat p, CompressionMode c)

We can see that this has 2 overloaded arguments which specifies the format and the compression mode. Let us see what this means.



































Value



Description



Base64Gif



The Graphics Interchange Format, which is then Base64-encoded, typically used for viewing ink as .mht files



Base64InkSerializedFormat



The ISF, which is then Base64-encoded, typically used for storing ink in XML



Gif



The Graphics Interchange Format, typically used for viewing ink in Web browsers



InkSerializedFormat



The Ink Serialized Format, typically used to save and load ink data

Advertisment

The Save method's optional CompressionMode value is used when saving in InkSerializedFormat. The table below shows the various types of compression available





























Value



Description



Default



The default compression mode-its value is Maximum



Maximum



The most compression possible (resulting in the smallest data size); also the most performance intensive



NoCompression



No compression occurs. This is the fastest performance

Now that we have seen how ink can be saved let us walk through an example and save the signature as a file so that it can be retrieved.

Advertisment

Start Visual Studio .NET 2003 and then create a new Windows Forms Application. Name the project SaveInk and then save it in a location of your choice.

Design the form so that it looks like this below.



Advertisment

This will have just 2 buttons and a text box. The text box will be used to capture the ink and the Save button will be used for saving the ink to a ISF file and the Retrieve button will be used for retrieving the data from the ISF file and displaying it on the screen.

Now let us start with some coding and get the application to work as per our requirement stated above.

Add this directive at the top of the page so that we can use Intellisense to get the properties and methods for the ink controls.

Advertisment

using

Microsoft.Ink;

we will also make reference to this namespace to enable saving and retrieving of files.

using

System.IO;

Advertisment

As discussed earlier about the InkOverlay class we will now create an instance of this InkOverlay class and then use it in our project. We will also see how this class is used with the various controls that is provided in the Windows Forms application

private

Microsoft.Ink.InkOverlay txtOverlay;

Now after this line of code

InitializeComponent

();

//



// TODO: Add any constructor code after InitializeComponent call



//





 



Place the following lines of code

//this is the where the initialization is done



txtOverlay

= new Microsoft.Ink.InkOverlay(textBox1.Handle);



txtOverlay

.Enabled=true;

Let us examine these lines of code and see what they mean. Earlier we created an object which is an instance of the class. The object txtOverlay is instantiated from the class InkOverlay provided with the Microsoft Ink SDK 1.7

This object can be bound with any control in Windows Forms and it is done using the handle of the control. So we have created the text box and then bound the handle of the textbox to the inkoverlay object that we created just now.

This will allow ink to be captured on the text box and this will be just ink and not lay any emphasis on the recognition of the ink but just the shape, colour and size of the ink that was created.

And once this is bound to the handle of the control it has to be enabled or else the ink feature will not work. We will see in a future article why this is necessary and the cases in which the overlay features have to be enabled or disabled.

That is all that is needed to enable inking on the text box. Now let us run the application and see if the inking can be done on the text box.

Press F5 and run the application and hold the pen over the text box. You can see that the cursor turns into a black point. Now start writing on the text box and voila it is now able to capture the ink as it is. This control is just a text box in the Windows Forms Collection and also used for just typing text in various applications that we have seen in the past. It is clear now that Microsoft has provided inking features for the existing controls in the Windows Forms Collection. This is seen in Figure 2.



Now let us focus on writing code so that the handwriting can be saved as an image and stored on the hard disk for future retrieval. We will also write code so that the file that we created can also be loaded back onto the application to be viewed.

Double click the button Save and we will now write the code in the event handler that is created.

private

void btnSave_Click(object sender, System.EventArgs e)



{



txtOverlay.Enabled = false;

// Choose the file to save



SaveFileDialog dlg = new SaveFileDialog();



dlg.DefaultExt = "isf";



dlg.Filter =



"ISF binary files (*.isf)|*.isf|All files (*.*)|*.*";

if (dlg.ShowDialog(this) == DialogResult.OK)



{



// Save the ink object to the file



Stream s = dlg.OpenFile();



byte <> buf = txtOverlay.Ink.Save(



PersistenceFormat.InkSerializedFormat);



s.Write(buf, 0, buf.Length);



}

txtOverlay.Enabled = true;

}

Let us go through the code that we wrote just now. It is very clear that we are making use of the System.IO namespace to provide functionality for the saving of files which everyone is familiar with. We have also used the filter properties of the Save dialog control to filter only for .isf format since the tablet pc supports only isf formats.

And then we create a stream of bytes and write the contents of the text box to the buffer. Finally we write the data from the buffer to the file in the disk.

That is all is needed to capture the ink in a file and save the file to the disk. Let us go ahead and then save this contents to the disk. It is clear that the open dialog allows us to save the files in the usual format that is familiar to most users.

Write some data on the text boxes and then click on the save button. The save dialog box opens. Choose a location for the file and then write a name for the file and then click on ok. This will save the file in the location that you mentioned in the format ".isf".

Now let us retrieve the file that we saved and see if the text will appear back on the text box. Click on the retrieve button, navigate to the folder where you just saved the file earlier. Then click on it and then select Ok. This text will now be seen on the text box. This demonstrates that ink can be saved as a file and retrieved whenever necessary.

The code for the retrieval of the file is shown below.

private

void btnRetrieve_Click(object sender, System.EventArgs e)



{



txtOverlay.Enabled = false;

// Choose the file to open



OpenFileDialog dlg = new OpenFileDialog();



dlg.DefaultExt = "isf";



dlg.Filter =



"ISF binary files (*.isf)|*.isf|All files (*.*)|*.*";

if (dlg.ShowDialog(this) == DialogResult.OK)



{



// Load the file into a new ink object



Stream s = dlg.OpenFile();



byte <> buf = new byte<s.Length>;



s.Read(buf, 0, buf.Length);



Ink newInk = new Ink();



newInk.Load(buf);



txtOverlay.Ink = newInk;

}

txtOverlay.Enabled = true;



}

We have seen a lot today and seen how the existing controls can be modified to include support for ink. We also saw how simple It was to save the ink as a file and then retrieve the file from disk as and when desired.

We will continue with the exploring of the Tablet PC in the forthcoming articles and also see more of ink features in the development of applications.



tech-news