Thursday 1 August 2013

How To Convert Text To Voice in WPF

How to  Convert Text To Voice in WPF ….






XAML Design Page

<Window x:Class ="Wpfwithdatabase.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >
    <Grid Background="#FF7BB8DE">
       
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="133,84,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="118" Visibility="Collapsed" />
        <TextBox Height="80" HorizontalAlignment="Left" Margin="123,43,0,0" Name="textBox1" VerticalAlignment="Top" Width="265" BorderBrush="#FF22EE22" Background="#CD335387" Foreground="White" FontWeight="Bold" FontSize="14" />
        <Button Content="Click To Listen Voice" Height="23" HorizontalAlignment="Left" Margin="182,129,0,0" Name="button2" VerticalAlignment="Top" Width="150" Click="button2_Click" Foreground="#FF1E48EA" />
        <Label Content="Enter Text Here" Height="28" HorizontalAlignment="Left" Margin="26,68,0,0" Name="label1" VerticalAlignment="Top" />
    </Grid>
</Window>

Code Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Speech.Synthesis;

namespace Wpfwithdatabase
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // object creation of SpeechSynthesizer Class
        private SpeechSynthesizer voice = new SpeechSynthesizer();
      
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // Code for select Female Voice
            voice.SelectVoiceByHints(VoiceGender.Female);

            //code for convert text to voice
            voice.SpeakAsync(textBox1.Text);
        }

      
      

      

      

      
    }
}


3 comments:

Adam said...

How to find 'System.Speech.Synthesis'
namespace?

Anonymous said...

I can't run this program coz 'System.Speech' namespace is not being accepted. What should I do?

Ramu kumar Gupta said...

Go to Add reff....
in Add Reference Dialogue box.
Click on .NET tab

then select System.Speech version 4.0.0.0
then click OK.