Pages

Friday, May 27, 2011

Code to create capcha image using javascript with sound effect

Here are few things that you need to do before you run this code.
1. Create a folder "SoundFiles" at the root of the project.
2. Add reference of "Interop.SpeechLib.dll" file so that it is added in the bin folder.
3. Place the "q.wav" and "sound.png" file at the root of the project.
Here is the code to create capcha image using javascript with sound effect
Default.aspx
<head runat="server"><title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function show()
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";

var string_length = 5;
var randomstring = '';
for (var i=0; i<string_length; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
var main = document.getElementById('txt1');
main.value = randomstring;
document.getElementById("Button1").click();

}
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" runat="server" id="txt1"  style="border-style: none; border-color: inherit;
            border-width: medium; background-color: black; color: red; font-family: 'Curlz MT';
            font-size: x-large; font-weight: bold; font-variant: normal; letter-spacing: 10pt;padding-left:10px;
            width: 135px;"  />
        <input type="button" onclick="show()" value="Change Capcha Image" />
        <img alt="" src="sound.png" onclick="javascript:playSound();" />      
    </div>
    <asp:TextBox ID="txtverification" runat="server"></asp:TextBox>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Verification" style="display:none" OnClick="btn1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Verification" OnClick="Button2_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="lblmsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
    <div id="divsound" ></div>
    </form>
</body>
Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string script = "<script language='javascript' type='text/javascript'>show();</script>";
ClientScript.RegisterStartupScript(this.GetType(), "script", script);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (txtverification.Text == txt1.Value)
{
lblmsg.Text = "Successfull";
}
else
{
lblmsg.Text = "Failure";
}
}
protected void btn1_Click(object sender, EventArgs e)
{
SpeechLib.SpFileStream spfilestream;
string WAVFileName=Convert.ToDateTime(DateTime.Now).ToString("MMddyyyymmss") + ".wav";
File.Copy(MapPath("q.wav"), MapPath("SoundFiles/" + WAVFileName), true);
string filename = MapPath("SoundFiles/" + WAVFileName);
spfilestream = new SpFileStreamClass();
spfilestream.Open(filename, SpeechStreamFileMode.SSFMCreateForWrite, false);
SpeechLib.ISpeechVoice ispvoice1 = new SpVoiceClass();
ispvoice1.AudioOutputStream = spfilestream;
ispvoice1.Speak(txt1.Value.ToString(), SpeechVoiceSpeakFlags.SVSFDefault);
ispvoice1.Rate =10000;
ispvoice1.WaitUntilDone(-1);
spfilestream.Close();
string script = "<script language='javascript' type='text/javascript'> "+
" function playSound() {"+
" document.getElementById('divsound').innerHTML=\"<embed src='SoundFiles/" + WAVFileName + "' hidden='true' autostart='true' loop='false' />\"" +
" } </script>";
ClientScript.RegisterStartupScript(this.GetType(), "script", script);
}

No comments:

Post a Comment