In this tutorial i am going to explain How to Create Captcha Image with Refresh Button in ASP.Net. or how to Generate captcha in asp.net c#. or Create your own captcha image generator in asp.net using c#.net. or how to create captcha code in c#. or how to generate captcha in asp.net with example. or generate a captcha code with numbers and alphabets (small and capital letters). or how to use captcha in asp.net c# with example. or how to use captcha in registration form in asp.net with using c#. or Generate Captcha and convert in to .jpeg format in asp.net c#.
How To Generate Captcha in Asp.Net - C# |
Now, Let's Generate captcha in Asp.Net C#
For that you need to create two deterrent web form one for Generate captcha and another for registration form.
First we create first CreateCaptcha.aspx
Now You just write code into CreateCaptcha.aspx.cs
GenerateCaptcha.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; public partial class CreateCaptcha : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Clear(); int height = 30; int width = 100; Bitmap bmp = new Bitmap(width, height); RectangleF rectf = new RectangleF(10, 5, 0, 0); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawString(Session["captcha"].ToString(), new Font("Thaoma", 12, FontStyle.Italic), Brushes.Green, rectf); g.DrawRectangle(new Pen(Color.Red), 1, 1, width - 2, height - 2); g.Flush(); Response.ContentType = "image/jpeg"; bmp.Save(Response.OutputStream, ImageFormat.Jpeg); g.Dispose(); bmp.Dispose(); } }
Now create Second Web form with named Registration.aspx and write aspx code for Registration Form.
Registration.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <style type="text/css"> .auto-style1 { height: 55px; color: #FFFFFF; } .auto-style2 { color: #FFFFFF; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="SM1" runat="server"> </asp:ScriptManager> <table style="border: solid 1px black; padding: 20px; position: relative; top: 50px;" align="center"> <tr> <td bgcolor="#009900" class="auto-style1" colspan="2" style="text-align: center"> <strong>WWW.ASPPOINT.COM</strong></td> </tr> <tr> <td> Email ID : </td> <td> <asp:TextBox ID="txtEmailID" runat="server" Width="200px"></asp:TextBox> </td> </tr> <tr> <td> Password : </td> <td> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox> </td> </tr> <tr> <td> Confirm Password : </td> <td> <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox> </td> </tr> <tr> <td> Enter Below Code : </td> <td> <asp:TextBox ID="txtCaptcha" runat="server" Width="200px"></asp:TextBox> </td> </tr> <tr> <td> </td> <td valign="middle"> <asp:UpdatePanel ID="UP1" runat="server"> <ContentTemplate> <table> <tr> <td style="height: 50px; width: 100px;"> <asp:Image ID="imgCaptcha" runat="server" Height="49px" /> </td> <td valign="middle"> <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" /> </td> </tr> </table> </ContentTemplate> </asp:UpdatePanel> </td> </tr> <tr> <td colspan="2" align="center"> <asp:Button ID="btnRegiser" runat="server" Text="Register" OnClick="btnRegister_Click" /> </td> </tr> <tr> <td colspan="2" align="center" bgcolor="#009933" class="auto-style2"> Designed and Developed by Nikunj Satasiya</td> </tr> </table> </div> </form> </body> </html>
After complete your .aspx code you need to write c# code in to Registration.aspx.cs file.
Registration.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; public partial class Registration : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillCaptcha(); } } void FillCaptcha() { try { Random random = new Random(); string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; StringBuilder captcha = new StringBuilder(); for (int i = 0; i < 6; i++) captcha.Append(combination[random.Next(combination.Length)]); Session["captcha"] = captcha.ToString(); imgCaptcha.ImageUrl = "GenerateCaptcha.aspx?" + DateTime.Now.Ticks.ToString(); } catch { throw; } } protected void btnRefresh_Click(object sender, EventArgs e) { FillCaptcha(); } protected void btnRegister_Click(object sender, EventArgs e) { if (Session["captcha"].ToString() != txtCaptcha.Text) Response.Write("Invalid Captcha Code"); else Response.Write("Valid Captcha Code"); FillCaptcha(); } }
If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon