Alessandro Del Sole's Blog

{ A programming space about Microsoft® .NET® }
posts - 1908, comments - 2047, trackbacks - 352

My Links

News

Your host

This is me! Questo spazio è dedicato a Microsoft® .NET®, di cui sono molto appassionato :-)

Cookie e Privacy

Disabilita cookie ShinyStat

Microsoft MVP

My MVP Profile

Microsoft Certified Professional

Microsoft Specialist

Xamarin Certified Mobile Developer

Il mio libro su VB 2015!

Pre-ordina il mio libro su VB 2015 Pre-ordina il mio libro "Visual Basic 2015 Unleashed". Clicca sulla copertina per informazioni!

Il mio libro su WPF 4.5.1!

Clicca sulla copertina per informazioni! E' uscito il mio libro "Programmare con WPF 4.5.1". Clicca sulla copertina per informazioni!

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.
If you're not an Italian user, please visit my English blog

Le vostre visite

I'm a VB!

Guarda la mia intervista a Seattle

Follow me on Twitter!

Altri spazi

GitHub
I miei progetti open-source su GitHub

Article Categories

Archives

Post Categories

Image Galleries

Privacy Policy

WPF: let your pc speak!

Windows Presentation Foundation allows computers to speak by using some features of the DirectX libraries according to Microsoft's implementation of Text-To-Speech technology. You just have to add a reference to the following assembly: System.Speech.dll.

Let's see how it works. Open Visual Studio 2005 and create a new Windows Application (WPF) project either in VB 2005 or in C#. Then add a reference to System.Speech.dll.

Let's draw a very simple button, which contains some text, writing the following XAML:

    <Grid>
      <Button Name="Button1" Width="250" Height="50" Click="Speak">Hello Windows Presentation Foundation!</Button>
    </Grid>

We assigned a Button1 identifier which is necessary to interact with the button via managed code. Moreover, when the user clicks on the button it will call the Speak method. Let's open the code-behind file and first of all add a Imports System.Speech.Synthesis/using System.Speech.Synthesis; directive. Then, type the following code inside the class:

Visual Basic:

    Dim synth As New SpeechSynthesizer

    Private Sub Speak(ByVal sender As Object, ByVal e As RoutedEventArgs)
        synth.Speak(Button1.Content.ToString)
    End Sub

Visual C#:

        SpeechSynthesizer synth = new SpeechSynthesizer();

        private void Speak(object sender, RoutedEventArgs e)
        {
            synth.Speak(Button1.Content.ToString());
        }

The class SpeechSyntesizer implements a lot of methods related to Text-To-Speech, but two are the most important: Speak and SpeakAsync which let computers speak by passing the specified string. The second one makes this operation in asyncronous manner. Try to launch the application and click the button... the magic appears! Your pc is speaking! And today you have discovered another exciting feature of Windows Presentation Foundation. Aren't you in love with it yet? :-)

Alessandro

Print | posted on lunedì 8 ottobre 2007 23:52 |

Powered by:
Powered By Subtext Powered By ASP.NET