Text to Speech using C# in Windows Forms Application

By | January 31, 2013

In this post I will explain how to convert text to speech using C# in Windows Forms Application

Download Source Code


In this post we will see how to convert text to speech (TTS) using a built in Narrator installed in the system. We will use SpeechSynthesizer class of System.Speech library to built a TTS application

In this application, we will add an option to select a voice from installed narrators and an option to play, pause and resume the speech. Audio of this speech can also be downloaded in Wave(.wav) format using a download button  

Step 1:

Create a new Windows Forms Application

Step 2:

Add reference to System.Speech library using Add Reference dialog box

Import following namespace in Form1.cs

using System.Speech.Synthesis;

Step 3:

Add following controls on Form1 and arrange them as shown in the first figure above

Step 4:

Declare following variable under Form1 class declaration

SpeechSynthesizer speaker = new SpeechSynthesizer();
bool flag = false;

SpeechSynthesizer is the class that we will use to make a text speak

Step 5:

Add following code in Form1 constructor

public Form1()
    speaker.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>
    btnPause.Enabled = false;
    foreach (InstalledVoice voice in speaker.GetInstalledVoices())

void speaker_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
    btnPlay.Enabled = true;
    btnPause.Enabled = false;

GetInstalledVoices method of the SpeechSynthesizer class returns all the voices or narrators installed in the system as a collection of InstalledVoice objects. It is bound then to the ComboBox to enable users to select a voice.

SpeakCompleted event triggered when a SpeechSynthesizer completes speaking. It is just used to enable or disable buttons.

Step 6:

Add following code in the click event of btnPlay

private void btnPlay_Click(object sender, EventArgs e)
    if (cbVoice.SelectedIndex >= 0)
        btnPlay.Enabled = false;
        btnPause.Enabled = true;
        MessageBox.Show("Please select a voice", "Text to Speech", 
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
private void Play(string words)

Play method is created to convert text send as parameter to speech. Here, SelectVoice of SpeechSynthesizer class is used to select the voice of a narrator. SelectVoiceByHints method can also be used to select a voice by specifying age, gender etc.

SpeakAsync method is used to convert text to speech and plays asynchronously. It takes string as input to speak out.

Step 7:

Add following code in the click event of btnPause

private void btnPause_Click(object sender, EventArgs e)
    if (speaker != null)
        if (flag == false)
            btnPause.Text = "Resume";
            flag = true;
            btnPause.Text = "Pause";
            flag = false;

Pause and Resume methods of SpeechSynthesizer class are used to play and pause the synthesizer respectively.

Step 8:

Add following code in the click event of btnDownload

private void btnDownload_Click(object sender, EventArgs e)
    FolderBrowserDialog browser = new FolderBrowserDialog();
    if (browser.ShowDialog() == DialogResult.OK)
        SpeechSynthesizer MySynthesizer = new SpeechSynthesizer();
        MySynthesizer.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>
        PromptBuilder builder = new PromptBuilder();

void MySynthesizer_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
    MessageBox.Show("Audio downloaded sucessfully", "Text to Speech", 
        MessageBoxButtons.OK, MessageBoxIcon.Information);

SetOutputToWaveFile method of SpeechSynthesizer class is used here to output text to a Wave file. It takes file location as parameter. When SpeakAsync is called with PromptBuilder, it writes output of the text given with prompt to a wave(.wav) file.

Thanks for reading!

0 thoughts on “Text to Speech using C# in Windows Forms Application

  1. Henry

    What did u write for the method speaker, as am getting red lines


    I am working on a project in which I have to merge (TEXT TO SPEECH) converted wave file with another wave file in to a single wave file which has to be played later. I am able to merge the wave files but problem is when I play the merged file , (TEXT TO SPEECH)converted wave file get played very fast which is not clear. what may be the problem..? programmatically(c#) how can I make the song to play in the same flow? how can I change the bit rate of merged wave file..? is there anything to do with the wavelength?


Leave a Reply

Your email address will not be published. Required fields are marked *