MY bLOG

Thursday, 14 November 2013

Captcha Image

In this article i would like to explain about captcha image code here
Right click on solution Explorer add new item
Code in .aspx pge:

<h1 align="center">captcha image</h1>
    <table align="center">
<tr>
<td>Prove You are not a robot:</td>
<td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        <td><asp:TextBox ID="txtrobocomp" runat="server"  Font-Names="Vivaldi" 
                        Font-Size="X-Large" Font-Bold="True" BackColor="#CC99FF" ForeColor="#CC0066" 
                        Width="148px" Height="33px"></asp:TextBox></td>
<td>
            <asp:ImageButton ID="ImageButton1" runat="server" 
                ImageUrl="~/images/14635845-recycling-reload-white-transparent-and-glossy-web-interface-icon-with-reflection.jpg" 
                Width="30px" Height="30px" AlternateText="refresh" 
                onclick="ImageButton1_Click" />
            </td>
<td>
                <asp:ImageButton ID="ImageButton2" runat="server"  
                    ImageUrl="~/images/download.jpg" Height="30px" Width="30px" 
                    onclick="ImageButton2_Click"/></td>
                    <td>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" 
                            ControlToValidate="TextBox1" ControlToCompare="txtrobocomp" 
                            ErrorMessage="Please enter correct Text" ForeColor="Red"></asp:CompareValidator></td>

            </tr>

            <tr><td colspan="6" align="center">
                <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" 

                    style="height: 26px" /></td></tr></table>

Code in .aspx.cs page: 

Add Namespace for string buider
 using sytem.Text;

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(RandomNumber(10, 99));
                builder.Append(RandomString(3, true));
                builder.Append(RandomString(2, false));
                txtrobocomp.Text = builder.ToString();
            }
        }
        private int RandomNumber(int min, int max)
        {
            Random rd = new Random();
            return rd.Next(min, max);
        }

        private string RandomString(int size, bool lowerCase)

        {
            StringBuilder builder = new StringBuilder();
            Random rd = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rd.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomNumber(10, 99));
            builder.Append(RandomString(3, true));
            builder.Append(RandomString(2, false));
            txtrobocomp.Text = builder.ToString();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

        {
            SpVoice sp = new SpVoice();
            sp.Volume = 90;
            sp.Speak(txtrobocomp.Text);
         
        }
hope you understand .
In this article we have seen captcha image using asp.net with refresh and speech text.

No comments:

Post a Comment