Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Master the Pen Using Ink Controls in Your Tablet Applications : Page 4

Although you can treat Microsoft's new Tablet PC Ink Data like image data, the Ink controls and objects provide a much wider variety of options for incorporating pen input into your applications. Learn how to persist ink in many formats, depending on your needs.


advertisement
Loading Ink from Files
Example 6: Opening Files Containing Ink Data

The btnOpen_Click() method handles the Open dialog. By creating a Stream object, you can call the load method that corresponds to the user's choice, passing the Stream object ByRef.

Private Sub btnOpen_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnOpen.Click [. . .] If myOpenFileDialog.ShowDialog() = _ DialogResult.OK Then Try ' Attempt to Open the file with ' read-only permission myStream = myOpenFileDialog.OpenFile() If Not myStream Is Nothing Then ' Just a readability move Dim filename As String = _ myOpenFileDialog.FileName.ToLower() ' Get the extension of the file Dim extension As String = _ filename.Substring _ (filename.LastIndexOf(".")) ' Use the extension to determine what ' form to save the data in Select Case extension Case ".isf" LoadISF(myStream) Case ".xml" LoadXML(myStream) Case Else ' If unfamiliar with the extension, ' assume ISF, the most "native format" LoadISF(myStream) End Select inkEdFileName.Text = filename 'repaint the inkEdit area inkEdFileName.Invalidate() Catch 'Display an error and exit MessageBox.Show("An error occurred while " & _ "loading ink from " & _ " the specified file. Please try again.",_ "Serialization", MessageBoxButtons.OK) Finally ' Always close the stream in the finally ' clause so it is always reached, regardless ' of whether an exception occurs. If Not myStream Is Nothing Then _ myStream.Close() End Try [. . .] End Sub

Example 7: Loading Ink from an ISF File

The LoadISF() method reads a Byte array from a previously created file and converts the Byte array to ink with the Ink object's Load() method. You should temporarily disable the ink collector while assigning the Ink object to it.

Private Sub LoadISF(ByVal s As Stream) 'Load ISF into the ink object Dim LoadedInk As Ink = New Ink() Dim isfBytes() As Byte = New Byte() {0} ReDim isfBytes(s.Length) ' read in the ISF s.Read(isfBytes, 0, CInt(s.Length)) ' Load the ink into a new ink object ' Once an ink object has been "dirtied" it can ' never load ink again LoadedInk.Load(isfBytes) ' temporarily disable the ink collector and ' swap ink objects myInkCollector.Enabled = False myInkCollector.Ink = LoadedInk myInkCollector.Enabled = True ' Repaint the inkable region gbInkArea.Invalidate() End Sub



Example 8: Loading Ink from an XML File

The LoadXML() subroutine loads a previously created XML file, retrieves data from the Ink node, and converts the data in the node to ink using the Ink object's Load method. You should temporarily disable the ink collector while assigning the Ink object to it. Call the Invalidate method of the ink collection box to force it to refresh..



Private Sub LoadXML(ByVal s As Stream) ' This function will load XML into the ink object. ' This object will encode our byte data to a ' UTF8 string Dim utf8 As UTF8Encoding = New UTF8Encoding() Dim myXD As XmlDocument = New XmlDocument() Dim myNodes As XmlNodeList Dim loadedInk As Ink = New Ink() ' Load the XML data into an XMLDocument object myXD.Load(s) ' Get the data in the ink node myNodes = myXD.GetElementsByTagName("Ink") ' load the ink into a new ink object ' once an ink object has been "dirtied" it can never ' load ink again If myNodes.Count <> 0 Then loadedInk.Load(utf8.GetBytes(myNodes(0).InnerXml)) ' temporarily disable the ink collector and ' swap ink objects myInkCollector.Enabled = False myInkCollector.Ink = loadedInk myInkCollector.Enabled = True ' Repaint the inkable region gbInkArea.Invalidate() End If End Sub

Example 9: Loading Ink from SQL Server

To load ink data from the SQL Server Database, you can reuse a little code. In the btnDisplay_Click() method, using the FileName column as the lookup value, you can use a simple data adapter to retrieve the information. Then you can use a Byte array and a Stream object to convert the data to ink format using the LoadISF() routine outlined earlier.

Private Sub btnDisplay_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnDisplay.Click ' Handles the Display button click event, allowing ' the user to display an image stored in the database. ' When nothing is selected in the ListBox, ' the SelectedIndex = -1. If lstInk.SelectedIndex < 0 Then MessageBox.Show("There are no ink selections " & _ "in the database to display.", _ "Empty Database!", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else ' The SQL Server Image datatype is a ' binary datatype. ' To generate ink from it, you must first create a ' stream object containing the binary data. Dim arrInk() As Byte = CType(dsInk.Tables(0). _ Rows(lstInk.SelectedIndex)("Picture"), Byte()) Dim myStream As New MemoryStream(arrInk) Try LoadISF(myStream) inkEdFileName.Text = dsInk.Tables(0).Rows _ (lstInk.SelectedIndex)("FileName").ToString Catch 'Display an error and exit MessageBox.Show("An error occurred while " & _ "loading ink. " & _ " Please verify the data selected " & _ "contains valid serialized " & _ "ink and try again.", "Serialization", _ MessageBoxButtons.OK) Finally ' Close the stream object to ' release the resource. If Not myStream Is Nothing Then myStream.Close() End If End Try End If End Sub

You can see that you handle ink in much the same way as you would handle image data. . You should review the Visual Design guidelines within the Tablet PC SDK before writing production ink application—especially those dealing with Tablet PC System Sizing Metrics for High DPI.

Since initial publication of this article, Microsoft has released an updated SDK as well as merged the Tablet PC API into the latest edition of Windows XP. Some features and functions in this article may therefore have changed and improved with the latest tools.



Tracy L. Bannon is a Senior Consultant with BearingPoint Inc (NYSE BE), one of the world's largest business consulting and systems integration firms. She has been working with VB system development for some years and has recently specialized in COM+ (VB6), MS SQL Server, and Tablet PC (.Net) development. She is a graduate of Penn State University. Click to reach Tracy by e-mail.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date