Silverlight 4 OOB – Handwriting Recognition & Text-To-Speech

I have been sitting on this sample for quite a while, but never found the time to get it ready for a post. Before I switch the focus of my blog over to Windows Phone 7 development, I wanted to get this out of the way first 🙂

With Silverlight 4, we introduced an exciting new capability for Out-Of-Browser apps: Trusted Applications - one of the key new fatures of Trusted Applications is the ability to integrate with COM Automation interfaces on your Windows PC. What does this mean? This allows you to call into native components on your system that are either provided by the OS or installed by the user or system admin. On MSDN you can find some sample code for the canonical example that highlights the power of this feature: integrating Microsoft Office features into your Silverlight application.

In this blog post, however, I want to show some other cool things you can do with these new APIs on your PC: I want to use the systems handwriting recognizer as well as the systems speech synthesizer within my Silverlight application. Let's do the easy part first and take a look at how we get the speech synthesis done (in the attached sample, this plays back the recognized handwriting):


private void btnSpeak_Click(object sender, RoutedEventArgs e)




    dynamic spVoice = AutomationFactory.CreateObject("SAPI.SpVoice.1");





In line 3 we create an instance of the SAPI.SpVoice (SAPI = Microsoft Speach API). Now we can just call the Speak() method (line 4) in order to have the desired text spoken by the machine. Make sure you turn on your speakers.

For the handwriting recognition part, the code looks a little bit more complicated, mostly for two reasons: The COM Automation API for Ink has some oddities & we need to convert the users strokes from pixel space into HiMetric. Here is the full code needed to implement this as extension method on Inkpresenter to have its writing recognized by the system's handwriting recognizer:


private const double HiMetricFactor = 26.46d; // = 1000 * 2.53 / 96


public static string Recognize(this InkPresenter presenter)




   dynamic recognizers = AutomationFactory.CreateObject("msinkaut.InkRecognizers");


   dynamic reco = recognizers.GetDefaultRecognizer(1033);


   dynamic context = reco.CreateRecognizerContext();


   dynamic inkCol = AutomationFactory.CreateObject("msinkaut.InkCollector");


   dynamic ink = inkCol.Ink;




   foreach (Stroke stroke in presenter.Strokes)




       int[] packets = new int[stroke.StylusPoints.Count * 2];


       for (int i = 0; i < stroke.StylusPoints.Count; i++)




           packets[i * 2] = (int)(stroke.StylusPoints[i].X * HiMetricFactor);


           packets[i * 2 + 1] = (int)(stroke.StylusPoints[i].Y * HiMetricFactor);




       dynamic inkStroke = ink.CreateStroke(packets, null);






   context.Strokes = ink.Strokes;


   dynamic result = context.Recognize(null);


   return result.TopString;



If you are a little lost, that's OK. Here is how/why it works: 🙂

On line 1 we just define the conversion factor for pixels to HiMetric (the coordinate space used by the native handwriting recognizer on the Windows system). Lines 4-6 are all about getting us a new RecognizerContext, which is the primary interface for the native recognition engines. In lines 6-7 I am using a little trick to get a new empty Ink object. There is no Automation-compliany way to produce one, but I can create an InkCollector that owns an Ink object. Then I can steal it for my purpose. All of lines 10-19 are about converting the Silverlight Stroke data (from pixels) into native Stroke data (in HiMetric). Last but not least, in lines 21-23 we pass the Strokes down to the recognizer have have them recognized synchronously.

The full source code for the sample is attached to this post. When you run it, please be sure to run it as Out-Of-Browser app, because Trusted Application features aren't supported in the browser in Silverlight 4.

Enjoy! 🙂

Comments (3)
  1. Nitin Shetty says:

    Have u got any idea related to text to speech conversion in WP7??

  2. azizou says:


    thank you for this post..i just want to know if i can interpret this code in windows phone7..if not, can you help me about "automation" and "AutomationFactory" and how i can use them in WP7

    thank you

  3. Duy Phuong says:

    impossible in windows phone 7.

    wp7 doesn't support Automation Factory

Comments are closed.

Skip to main content