In this article, we will learn how to create a Text to Speech Converter in a C# Windows Forms application using the SpeechSynthesizer class.

Let's Begin

Step 1

Create a new project then select Windows Forms application in C# and give it a name.



Step 2

Drop a RichTextBox and four Button Controls from the ToolBox.



Step 3

Right-click on the References Folder then click on Add Reference.



Step 4

Select the System.Speech Assembly and click on OK.



After adding it, you will see System.Speech assembly under the References folder.



Step 5

Go to Form1.cs (code behind .cs file) and add System.Speech (the System.Speech namespace contains types that support speech recognition) and System.Speech.Synthesis namespace (the System.Speech.Synthesis namespace contains classes for initializing and configuring a speech synthesis engine and for generating speech and so on).

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Speech;
  4. using System.Speech.Synthesis;
  5. namespace TextToSpeechConverter
  6. {
  7. public partial class Form1 : Form
  8. {
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13. //SpeechSynthesizer Class Provides access to the functionality of an installed a speech synthesis engine.
  14. SpeechSynthesizer speechSynthesizerObj;
  15. private void Form1_Load(object sender, EventArgs e)
  16. {
  17. speechSynthesizerObj = new SpeechSynthesizer();
  18. btn_Resume.Enabled = false;
  19. btn_Pause.Enabled = false;
  20. btn_Stop.Enabled = false;
  21. }
  22. private void btn_Speak_Click(object sender, EventArgs e)
  23. {
  24. //Disposes the SpeechSynthesizer object
  25. speechSynthesizerObj.Dispose();
  26. if(richTextBox1.Text!="")
  27. {
  28. speechSynthesizerObj = new SpeechSynthesizer();
  29. //Asynchronously speaks the contents present in RichTextBox1
  30. speechSynthesizerObj.SpeakAsync(richTextBox1.Text);
  31. btn_Pause.Enabled = true;
  32. btn_Stop.Enabled = true;
  33. }
  34. }
  35. private void btn_Pause_Click(object sender, EventArgs e)
  36. {
  37. if(speechSynthesizerObj!=null)
  38. {
  39. //Gets the current speaking state of the SpeechSynthesizer object.
  40. if(speechSynthesizerObj.State==SynthesizerState.Speaking)
  41. {
  42. //Pauses the SpeechSynthesizer object.
  43. speechSynthesizerObj.Pause();
  44. btn_Resume.Enabled = true;
  45. btn_Speak.Enabled = false;
  46. }
  47. }
  48. }
  49. private void btn_Resume_Click(object sender, EventArgs e)
  50. {
  51. if (speechSynthesizerObj != null)
  52. {
  53. if (speechSynthesizerObj.State == SynthesizerState.Paused)
  54. {
  55. //Resumes the SpeechSynthesizer object after it has been paused.
  56. speechSynthesizerObj.Resume();
  57. btn_Resume.Enabled = false;
  58. btn_Speak.Enabled = true;
  59. }
  60. }
  61. }
  62. private void btn_Stop_Click(object sender, EventArgs e)
  63. {
  64. if(speechSynthesizerObj!=null)
  65. {
  66. //Disposes the SpeechSynthesizer object
  67. speechSynthesizerObj.Dispose();
  68. btn_Speak.Enabled = true;
  69. btn_Resume.Enabled = false;
  70. btn_Pause.Enabled = false;
  71. btn_Stop.Enabled = false;
  72. }
  73. }
  74. }
  75. }
Final Preview



I hope you like this. Thanks.