March 16, 2011

Creating a Captcha Control – Part 3

Adding Audio

In my previous 2 articles – Creating a Captcha Control – Part 1 and Creating a Captcha Control – Part 2 – I have created the user control for the captcha control. The control also allows functionality to play audio to read the text on the captcha.

To achieve the audio feature, I have used a COM library – Microsoft Speech Object Library. To add the COM library, I added a reference to Microsoft Speech Object Library (%Windir%\System32\Speech\Common\Sapi.dll). To add a reference, right-click on the Reference folder, click Add Reference, click COM tab and add the library.

captcha-add-reference

Once the reference is added, the SpeechLib namespace is imported as shown below.

Adding reference to SpeechLib
  1. using SpeechLib;

Then, the Speak method in SpVoice class is used to play the audio as per below.

ReadCaptcha method
  1. protected void ReadCaptcha(object sender, EventArgs e)
  2. {
  3.     SpVoice voice = new SpVoice();
  4.  
  5.     char[] text = ((string)ViewState["captcha"]).ToCharArray();
  6.     
  7.  
  8.     for (int i = 0; i < text.Length; i++)
  9.     {
  10.         voice.Speak(text[i].ToString(), SpeechVoiceSpeakFlags.SVSFDefault);
  11.     }
  12. }

As can be seen, I am reading out each of the text and not all the text at once. The reason is, SpVoice class will try to read it as a word instead of individual characters otherwise.

In the following article, I will show how to create and use the handler to generate the image. The whole code for the captcha control can be downloaded from CodePlex at http://captchadotnet.codeplex.com/

2 comments:

Chris said...

Are you using the SAPI COM library to circumvent needing the application pool to run under full trust in order to use the managed System.Speech classes?

ASP.NET Captcha Control said...

You can also try our ASP.NET captcha control with sound

Reference: Shahed Kazi at AspNetify.com