Monday, March 24, 2008

Free Captcha Image - ASP.NET,C# - with source code

Captcha Image - ASP.NET and C#

simple code for Captcha image in C# and ASPNET.
source code for image Captcha this a Class file U can pass value as string into
DrowImage return a image. You can write that image. and point that page as source of your image.
it shown under this code. it also include code for Generating Randam Stirng in C#,ASP.NET the function
RandomStringKeys() use for Generation of Randam Strings.

Class file Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Text;
using System.Collections;
namespace ThreekCaptcha
public class ThreeKcaptcha
public ThreeKcaptcha()
{ }
public Bitmap Image
return this.image;
private Bitmap image;
public void DrowImage(string key)
Bitmap bitmapImage = new Bitmap(150, 30, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmapImage);
g.SmoothingMode = SmoothingMode.HighQuality;
Rectangle rect = new Rectangle(0, 0, 150, 30);
HatchBrush hBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
g.FillRectangle(hBrush, rect);
hBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.Red, Color.Black);
float fontSize = 20;
Font font = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Strikeout);
float x = 10;
float y = 1;
PointF fPoint = new PointF(x, y);
g.DrawString(key, font, hBrush, fPoint);
image = bitmapImage;

public string RandomStringKeys()
string returnString = string.Empty;
string Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string numbers = "0123456789";
char[] letterArray = Letters.ToCharArray();
char[] numberArray = numbers.ToCharArray();
Random objrandomNumber = new Random();
byte[] selection = Encoding.ASCII.GetBytes(objrandomNumber.Next(111111, 999999).ToString());
BitArray bArray = new BitArray(selection);

string binaryString = string.Empty;
int t = 0;
int f = 0;

for (int i = 0; i < encodestring =" binaryString.Substring(objrandomNumber.Next(1," i =" 0;" style="font-weight: bold;" href="">Download Link For Captcha DLL

source for Image

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 _kImageCaptcha;
using System.Drawing.Imaging;
using ThreekCaptcha;
public partial class Default2 : System.Web.UI.Page
ThreeKcaptcha obj3kCaptcha;
protected void Page_Load(object sender, EventArgs e)
obj3kCaptcha = new ThreeKcaptcha();
string tstKey = obj3kCaptcha.RandomStringKeys();
Response.ContentType = "image/jpeg";
obj3kCaptcha.Image.Save(Response.OutputStream, ImageFormat.Jpeg);

Download Full Source Code

No comments: