Ink and Handwriting Recognition C#

Requirements:

1.      
The microsoft tablet SDK, which should include
the Microsoft INK DLL http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=080184dd-5e92-4464-b907-10762e9f918b&displayLang=en

2.      
Extract the files and add a reference to the
Microsoft.INK dll to your project like so:

3.      
Also, add the following controls to your toolbox
by right-clicking the toolbox, selecting “Choose Items..” from the popup menu
and locating these controls

4.      
Create a form with an InkPicture control and
some buttons

 

Code to Clear the handwriting
area:

private void
btnClear_Click(object sender, EventArgs e)

        {

           
Strokes buburahin = inpInput.Ink.Strokes;

           
if (!inpInput.CollectingInk)

           
{

        
   inpInput.Ink.DeleteStrokes(buburahin);

           
textBox1.Text = "";

           
}

           
Refresh();

           

        }

Code to Open an existing isf file.

private void
btnOpen_Click(object sender, EventArgs e)

        {

           
OpenFileDialog opendlg = new OpenFileDialog();

           
opendlg.Filter = "ISF files (*.isf)|*.isf| All
files (*.*)|*.*"
;

           
if (opendlg.ShowDialog() == DialogResult.OK)

           
{

               
inpInput.InkEnabled = false;

               
Stream stream = opendlg.OpenFile();

               
if (stream != null
&& stream.Length > 0)

               
{

                   
Byte[] bytes = new
byte[stream.Length];

                   
if (stream.Read(bytes, 0, bytes.Length) ==
stream.Length)

                   
{

                    
   Ink ink = new Ink();

                       
ink.Load(bytes);                       

                       
inpInput.Ink = ink;

                       
stream.Close();

                   
}

                  

               
}

           
}

        
   inpInput.InkEnabled = true;

        }

 

·        
Take note that the file is opened as a stream
object. This can be trated as the same with SQL

 

Code to Save the written strokes

private void
btnSave_Click(object sender, EventArgs e)

        {

           
SaveFileDialog myOFD = new SaveFileDialog();

           
Stream imageStream;

           
myOFD.Filter = "Ink Serialized Format files
(*.isf)|*.isf"
;

           
byte[] isf;

           
if (myOFD.ShowDialog() == DialogResult.OK)

           
{

               
imageStream = myOFD.OpenFile();

               
string FileName = myOFD.FileName.ToLower();

 

               
isf = inpInput.Ink.Save(PersistenceFormat.InkSerializedFormat);

               
imageStream.Write(isf, 0, isf.Length);

               
imageStream.Close();

           
}

           

       
}

 

 

Code for handwriting recognition

 

private void
button1_Click(object sender, EventArgs e)

        {

           
textBox1.Text = inpInput.Ink.Strokes.ToString();

       
}

 

Enjoy!

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Ink and Handwriting Recognition C#

  1. Dude! I have to ask you a question about an issue im having with the clearing of ink strokes method you listed above.

    The application is a delphi app and every now and then the application refuses to clear the ink strokes (InkPicture by the way). I get this error:
    “The operation cannot be performed while the user is actively inking or recognizing”

    Now the application has finished recognising and the inking process has finished.
    Is there somthing I am missing in the process??? The error is Random as well but is now becoming a major problem as the software is released amongst hundreds of users and i cannot find a solution short of destroying the forms and re-creating them each time they are used (want to avoid doing that if i can)

    • ezlorenzo says:

      hi BMC,

      Am not really well versed in delphi but based on the error message you got it looks like you forgot to check if it is still writing(inking) before clearing it

      if (!inpInput.CollectingInk)
      {
      inpInput.Ink.DeleteStrokes(buburahin);
      }

      Here the code checks if inpInput is NOT collecting ink before calling .DeleteStrokes

      HTH!

  2. Its ok, Delphi or not, the methods are still the same.

    The problem is that im not sure if there is some commmand i should be calling when a person finished filling out a form. Should i be instructing the fields to stop collecting ink or not or is this done after leaving the fields focus. Or maybe its staying in collection mode because of a hardware fault.

    Its very difficult to replicate in a dev environment being so random

  3. ezlorenzo says:

    your first line of defense would be to capture the error so it won’t stall the app.

    I did two things on purpose with the code I posted as a reply above. First, I purposely used NOT CollectingInk because the control will determine if it is busy or not by itself. You might want to call this pessimistic coding, the act of assuming an event is finished. If I were to hazard a guess, there might be some configuration stuff you will need to tweak on Dev machines. Of might just be a simple case of button placement on the screen? Am thinking the “Clear” button is being clicked while the user is writing.

    This is a pretty old post, but as far as I can recall, I tried out the Stroke event before the CollectingInk. I know I have a good reason why I chose to ask if the object is not collecting anymore as opposed to asking of the object is “being stroked” iww… 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s