Breaking News

Editors Picks

Thursday, September 29, 2011

Captcha like google

Create Page Turing.aspx


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Security.Cryptography;
using System.Text;

namespace sunilcaptcha
{
public partial class Turing : System.Web.UI.Page
{
private static byte[] randb = new byte[4];
private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();

private FontFamily[] fonts = {
new FontFamily("Times New Roman"),
new FontFamily("Georgia"),
new FontFamily("Arial"),
new FontFamily("Comic Sans MS")
};
protected void Page_Load(object sender, EventArgs e)
{

int width = 200;
int hieght = 80;
Random random = new Random();
int ImageNum = RandomNumber(0, 8); // (1-max number of background images)


// Load image
//  Bitmap bitMapImage = new System.Drawing.Bitmap(filename);
Bitmap bitMapImage = new Bitmap(width, hieght, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitMapImage);
Color bc = Color.White;
// Get random string from RandomString(int sizeOfString) method
string textToWrite = RandomString(8);

Rectangle rect = new Rectangle(0, 0, width, hieght);
g.SmoothingMode = SmoothingMode.AntiAlias;

using (SolidBrush b = new SolidBrush(bc))
{
g.FillRectangle(b, rect);
}

// Set up the text font.
int emSize = (int)(width * 5 / textToWrite.Length);
FontFamily family = fonts[Next(fonts.Length - 1)];
Font font = new Font(family, emSize);
// Adjust the font size until the text fits within the image.
SizeF measured = new SizeF(0, 0);
SizeF workingSize = new SizeF(width, hieght);
while (emSize > 2 &&
(measured = g.MeasureString(textToWrite, font)).Width > workingSize.Width ||
measured.Height > workingSize.Height)
{
font.Dispose();
font = new Font(family, emSize -= 2);
}

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;

// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString(textToWrite, font.FontFamily, (int)font.Style, font.Size, rect, format);

// Set font color to a color that is visible within background color

int bcR = Convert.ToInt32(bc.R);
// This prevents font color from being near the bg color
int red = Next(255), green = Next(255), blue = Next(255);
while (red >= bcR && red - 20 <= bcR ||
red < bcR && red + 20 >= bcR)
{
red = Next(0, 255);
}
//red=26 green=237, blue=
SolidBrush sBrush = new SolidBrush(Color.FromArgb(0, 201, 87));
g.FillPath(sBrush, path);

// Iterate over every pixel
double distort = Next(5, 15) * (Next(10) == 1 ? 1 : -1);

// Copy the image so that we're always using the original for source color
using (Bitmap copy = (Bitmap)bitMapImage.Clone())
{
for (int y = 0; y < hieght; y++)
{
for (int x = 0; x < width; x++)
{
// Adds a simple wave
int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 44.0)));
if (newX < 0 || newX >= width) newX = 0;
if (newY < 0 || newY >= hieght) newY = 0;
bitMapImage.SetPixel(x, y, copy.GetPixel(newX, newY));
}
}
}

Session["randomStr"] = textToWrite.ToLower(); //Encoding.UTF8.GetString(md5.ComputeHash(pass));


System.IO.MemoryStream mstr = new System.IO.MemoryStream();
// Save the image
bitMapImage.Save(mstr, ImageFormat.Jpeg);

// Set the content type
Response.ContentType = "image/png";
// Write image
Response.BinaryWrite((byte[])(mstr).ToArray());

//Clean
font.Dispose();
sBrush.Dispose();
g.Dispose();
bitMapImage.Dispose();
}

public string GetRandomPasswordUsingGUID(int length)
{
// Get the GUID
string guidResult = System.Guid.NewGuid().ToString();

// Remove the hyphens
guidResult = guidResult.Replace("-", string.Empty);

// Return the first length bytes
return guidResult.Substring(0, length);
}

private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}

private string RandomString(int size)
{
string s = "";
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
int index;
int lenght = Next(8, 8);
for (int i = 0; i < lenght; i++)
{
index = Next(chars.Length - 1);
s += chars[index].ToString();
}
return s;

}
public  int Next()
{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
if (value < 0) value = -value;
return value;
}
///



/// Generates a random non-negative number less than or equal to max.
///

/// The maximum possible value.
public  int Next(int max)
{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
value = value % (max + 1); // % calculates remainder
if (value < 0) value = -value;
return value;
}
///



/// Generates a random non-negative number bigger than or equal to min and less than or
///  equal to max.
///

/// The minimum possible value.
/// The maximum possible value.
public  int Next(int min, int max)
{
int value = Next(max - min) + min;
return value;
}
}
}


after the add captcha register.aspx page


<%@ Page Language="C#" AutoEventWireup="true" Codebehind="register.aspx.cs" EnableEventValidation="false"
Inherits="register" %>

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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>captchatitle>



<script language="Javascript" type="text/javascript">

function refreshSecuritylogin()
{
var randomnumber=Math.floor(Math.random()*1001);
document.images["refreshImg2"].src = "Turing.aspx" + '?rand=' + randomnumber; ;
}
script>
head>
<body >
<form id="form1" runat="server" method="post">
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>
<td align="left" valign="middle">
<table style="width: 100%">
<tr>
<td width="33%" align="left" height="40" valign="middle" id="Code">
Security Codetd>
<td width="67%" align="left" valign="middle">
<asp:TextBox CssClass="text_field" ID="txtSecurityCode" runat="server" TabIndex="13"
AutoCompleteType="None" MaxLength="8" Style="width: 160px; height: 25px;">asp:TextBox>
<asp:LinkButton ID="lnkRefreshCode" runat="server" OnClick="lnkRefreshCode_Click"
TabIndex="40">
<asp:Label ID="lblRefreshCode" runat="server" Text="Refresh Code" Visible="false" />
asp:LinkButton>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSecurityCode"
ErrorMessage="Enter Security Code" ValidationGroup="SignUPpop" Display="none">*asp:RequiredFieldValidator>
td>
tr>
<tr>
<td align="left" valign="top">
td>
<td align="left" valign="top">
<img src="Turing.aspx" class="text_field" alt="refreshImg" name="refreshImg"
id="refreshImg"  height="50px"  width="160px"/>
<br />
<a href="#" onclick="return refreshSecurity();" tabindex="14">Refresh Codea>
td>
tr>
table>                                                td>
<td height="40" align="left" valign="middle">
<asp:ImageButton ID="imgbtnsubmit" runat="server" ValidationGroup="RegisterSignUP"
TabIndex="15" ImageUrl="images/register_2.png" Width="62" Height="28" border="0"
OnClick="imgbtnsubmit_Click" OnClientClick="return ShowModalDialog()" />
<asp:ImageButton ID="ImageButton1" runat="server" Width="49" Height="28" ImageUrl="images/reset.png"
alt="send" border="0" OnClick="ImageButton1_Click" />
td>
tr>
<tr>
<td align="left" valign="middle">

td>
<td align="left" valign="middle">

td>
tr>
table>
div>
div>
td>
tr>
<tr>
<td align="left" valign="top">

td>
tr>
table>
td>
tr>
table>

form>
body>
html>



Read more ...

Contact Us

Name

Email *

Message *