Handwriting Recognition
This section discusses the fundamentals of recognition as it pertains to digital ink in the WPF platform.
Recognition Solutions
The following example shows how to recognize ink using the Microsoft.Ink.InkCollector class.
Note
This sample requires that handwriting recognizers be installed on the system.
Create a new WPF application project in Visual Studio called InkRecognition. Replace the contents of the Window1.xaml file with the following XAML code. This code renders the application's user interface.
<Window x:Class="InkRecognition.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="InkRecognition"
>
<Canvas Name="theRootCanvas">
<Border
Background="White"
BorderBrush="Black"
BorderThickness="2"
Height="300"
Width="300"
Canvas.Top="10"
Canvas.Left="10">
<InkCanvas Name="theInkCanvas"></InkCanvas>
</Border>
<TextBox Name="textBox1"
Height="25"
Width="225"
Canvas.Top="325"
Canvas.Left="10"></TextBox>
<Button
Height="25"
Width="75"
Canvas.Top="325"
Canvas.Left="235"
Click="buttonClick">Recognize</Button>
<Button x:Name="btnClear" Content="Clear Canvas" Canvas.Left="10" Canvas.Top="367" Width="75" Click="btnClear_Click"/>
</Canvas>
</Window>
Add a reference to the Microsoft Ink assembly, Microsoft.Ink.dll, which can be found in \Program Files\Common Files\Microsoft Shared\Ink. Replace the contents of the code behind file with the following code.
using System.Windows;
using Microsoft.Ink;
using System.IO;
namespace InkRecognition
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
// Recognizes handwriting by using RecognizerContext
private void buttonClick(object sender, RoutedEventArgs e)
{
using (MemoryStream ms = new MemoryStream())
{
theInkCanvas.Strokes.Save(ms);
var myInkCollector = new InkCollector();
var ink = new Ink();
ink.Load(ms.ToArray());
using (RecognizerContext context = new RecognizerContext())
{
if (ink.Strokes.Count > 0)
{
context.Strokes = ink.Strokes;
RecognitionStatus status;
var result = context.Recognize(out status);
if (status == RecognitionStatus.NoError)
textBox1.Text = result.TopString;
else
MessageBox.Show("Recognition failed");
}
else
{
MessageBox.Show("No stroke detected");
}
}
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
theInkCanvas.Strokes.Clear();
}
}
}
Imports System.Windows
Imports Microsoft.Ink
Imports System.IO
'/ <summary>
'/ Interaction logic for Window1.xaml
'/ </summary>
Namespace InkRecognition
Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
' Recognizes handwriting by using RecognizerContext
Private Sub buttonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Using ms As New MemoryStream()
theInkCanvas.Strokes.Save(ms)
Dim myInkCollector As InkCollector = New InkCollector()
Dim ink As Ink = New Ink()
ink.Load(ms.ToArray())
Using context As New RecognizerContext()
If ink.Strokes.Count > 0 Then
context.Strokes = ink.Strokes
Dim status As RecognitionStatus
Dim result As RecognitionResult = context.Recognize(status)
If status = RecognitionStatus.NoError Then
textBox1.Text = result.TopString
Else
MessageBox.Show("Recognition failed")
End If
Else
MessageBox.Show("No stroke detected")
End If
End Using
End Using
End Sub
Private Sub btnClear_Click(sender As Object, e As RoutedEventArgs)
theInkCanvas.Strokes.Clear()
End Sub
End Class
End Namespace
See also
.NET Desktop feedback