latest Post

CAPTCHA in C#

Create a new website in Visual Studio and add two files in it.

Default.aspx
GenerateCaptcha.aspx
Add following namespaces and write code below in Page Load event of GenerateCaptcha.aspx.cs file.

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
Response.Clear();
int height = 35;
int width = 80;
Bitmap objBmp = new Bitmap(width, height);
RectangleF rectf = new RectangleF(10, 5, 0, 0);
Graphics objGraphics = Graphics.FromImage(objBmp);
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; objGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality; objGraphics.DrawString(Session["CaptchaCode"].ToString(), new Font("Chiller", 18, FontStyle.Italic), Brushes.Blue, rectf);
objGraphics.DrawRectangle(new Pen(Color.Green), 1, 1, width - 2, height - 2);
bjGraphics.Flush();
Response.ContentType = "image/jpeg";
objBmp.Save(Response.OutputStream, ImageFormat.Jpeg); objGraphics.Dispose(); objBmp.Dispose();
Now add code below in Default.aspx page
<asp:scriptmanager id="ScriptManager1" runat="server"><asp:scriptmanager>
<table>
    <tbody>
<tr>
        <td valign="middle"><asp:updatepanel id="UpdatePanel1" runat="server">
                <contenttemplate>
                    <table>
                        <tbody>
<tr>
                            <td style="height: 50px; width: 100px;"><asp:image id="imgCaptchaCode" runat="server">
                            </asp:image></td>
                            <td valign="middle"><asp:button id="btnRefreshCode" onclick="btnRefreshCode_Click" runat="server" text="Refresh Code">
                            </asp:button></td>
                        </tr>
</tbody></table>
</contenttemplate>
            </asp:updatepanel>
        </td>
    </tr>
<tr>
        <td>Enter above code:
            <asp:textbox id="txtCaptchaCode" runat="server" width="100px"></asp:textbox>
        </td>
    </tr>
<tr>
        <td align="center" colspan="2"><asp:button id="btnOk" onclick="btnOk_Click" runat="server" text="OK" width="98px">
        </asp:button></td>
    </tr>
</tbody></table>

Write following code in Default.aspx.cs file

 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 _Default : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
 if (!IsPostBack) {
 FillCapcthaCode();
 }
 }
 void FillCapcthaCode() {
 try {
 Random objRandom = new Random();
 string combination = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder captchaCode = new StringBuilder(); for (int i = 0; i < 5; i++) { captchaCode.Append(combination[objRandom.Next(combination.Length)]);
 }
 Session["CaptchaCode"] = captchaCode.ToString();
 imgCaptchaCode.ImageUrl = "GenerateCaptcha.aspx?" + DateTime.Now.Ticks.ToString();
 }
 catch(Exception ex) {
 Response.Write(ex.Message); }
 }
 protected void btnRefreshCode_Click(object sender, EventArgs e)
 {
 FillCapcthaCode();
}
 protected void btnOk_Click(object sender, EventArgs e) {
 if (Session["CaptchaCode"].ToString() != txtCaptchaCode.Text) {
 Response.Write("Please write valid Code");
 }
 else {
 Response.Write("You have entered valid code");

 } 
txtCaptchaCode.Text = string.Empty; 
FillCapcthaCode();
 }
 }

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment