Windows Forms: Text to speech in C#

This demo explains how to convert text to speech using system.speech library in c# .net windows forms application.

As you know, Microsoft .NET framework provides System.Speech.Synthesis for voice synthesis. To play the demo, you can drag the Label, Combobox, TextBox and Button controls from the visual studio toolbox to your winform, then layout your UI as shown below allows you to select the voice from the combobox, then generate the voice from the text or save the voice to the file.

text to speech in c#

First, you should create the voice variable.

SpeechSynthesizer voice;

Next, Double click on your form, then add code to the Load event handler allows you to create new instance to the voice variable.

private void frmMain_Load(object sender, EventArgs e)
{
    voice = new SpeechSynthesizer();
}

Add the click event handler to the Speak button allows you to create voice from the text as the following c# code.

private void btnSpeak_Click(object sender, EventArgs e)
{
    try
    {
        //Select voice
        switch (cboSelectVoice.SelectedIndex)
        {
            case 0:
                voice.SelectVoiceByHints(VoiceGender.NotSet);
                break;
            case 1:
                voice.SelectVoiceByHints(VoiceGender.Male);
                break;
            case 2:
                voice.SelectVoiceByHints(VoiceGender.Female);
                break;
            case 3:
                voice.SelectVoiceByHints(VoiceGender.Neutral);
                break;
            default:
                break;
        }
        voice.SpeakAsync(txtContent.Text);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Add the click event handler to the Pause button allows you to pause the speech.

private void btnPause_Click(object sender, EventArgs e)
{
    try
    {
        voice.Pause();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Add the click event handler to the Resume button allows you to resume the speech.

private void btnResume_Click(object sender, EventArgs e)
{
    try
    {
        voice.Resume();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

To save the voice to file, you can call the SetOutputToWaveStream method.

private void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        //Save text to wav file
        using (SaveFileDialog sfd = new SaveFileDialog())
        {
            sfd.Filter = "Wav files|*.wav";
            sfd.Title = "Save to a wave file";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
                voice.SetOutputToWaveStream(fs);
                voice.Speak(txtContent.Text);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

The c# example robot voice text to speech that helps you integrate into your video, you can also install the natural text to speech, then select the voice to play the demo.

VIDEO TUTORIAL