Code forUI design Part
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CaptchaControl.aspx.cs" Inherits="CaptchaControl" %>
<!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 runat="server">
<title>CaptchaImagesControl Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
code behind part in CaptchaImages Control
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;
using System.Drawing.Imaging;
using System.Text;
public partial class CaptchaControl : System.Web.UI.Page
{
private Random rand = new Random();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateImage();
}
}
private void CreateImage()
{
string code = GetRandomText();
Bitmap bitmap = new Bitmap(130, 25, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Yellow);
Rectangle rect = new Rectangle(0, 0, 130, 25);
SolidBrush b = new SolidBrush(Color.LightGray);
SolidBrush blue = new SolidBrush(Color.Blue);
//SolidBrush b2 = new SolidBrush(Color.FromArgb((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))));
//SolidBrush b3 = new SolidBrush(System.Drawing.Color.LightGray);
//SolidBrush blue = new SolidBrush(Color.FromArgb((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))));
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(b, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Times New Roman", 1 + rand.Next(12, 14)), blue, new PointF(counter + 1, 1));
counter += 20;
}
DrawRandomLines(g);
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
g.Dispose();
bitmap.Dispose();
}
private void DrawRandomLines(Graphics g)
{
//SolidBrush green = new SolidBrush(Color.Green);
SolidBrush green = new SolidBrush(Color.FromArgb((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))));
for (int i = 0; i < 7; i++)
{
g.DrawLines(new Pen(green, 1), GetRandomPoints());
}
}
private Point[] GetRandomPoints()
{
Point[] points = { new Point(rand.Next(5, 200), rand.Next(5, 200)), new Point(rand.Next(5, 100), rand.Next(5, 100)) };
return points;
}
private string GetRandomText()
{
StringBuilder randomText = new StringBuilder();
if (Session["Code"] == null)
{
string alphabets = "abcdefghijklmnpqrstuvwxyz0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
Random r = new Random();
for (int j = 0; j <= 5; j++)
{
randomText.Append(alphabets[r.Next(alphabets.Length)]);
}
Session["Code"] = randomText.ToString();
}
return Session["Code"] as String;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CaptchaControl.aspx.cs" Inherits="CaptchaControl" %>
<!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 runat="server">
<title>CaptchaImagesControl Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
code behind part in CaptchaImages Control
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;
using System.Drawing.Imaging;
using System.Text;
public partial class CaptchaControl : System.Web.UI.Page
{
private Random rand = new Random();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateImage();
}
}
private void CreateImage()
{
string code = GetRandomText();
Bitmap bitmap = new Bitmap(130, 25, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Yellow);
Rectangle rect = new Rectangle(0, 0, 130, 25);
SolidBrush b = new SolidBrush(Color.LightGray);
SolidBrush blue = new SolidBrush(Color.Blue);
//SolidBrush b2 = new SolidBrush(Color.FromArgb((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))));
//SolidBrush b3 = new SolidBrush(System.Drawing.Color.LightGray);
//SolidBrush blue = new SolidBrush(Color.FromArgb((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))));
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(b, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Times New Roman", 1 + rand.Next(12, 14)), blue, new PointF(counter + 1, 1));
counter += 20;
}
DrawRandomLines(g);
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
g.Dispose();
bitmap.Dispose();
}
private void DrawRandomLines(Graphics g)
{
//SolidBrush green = new SolidBrush(Color.Green);
SolidBrush green = new SolidBrush(Color.FromArgb((rand.Next(0, 255)), (rand.Next(0, 255)), (rand.Next(0, 255))));
for (int i = 0; i < 7; i++)
{
g.DrawLines(new Pen(green, 1), GetRandomPoints());
}
}
private Point[] GetRandomPoints()
{
Point[] points = { new Point(rand.Next(5, 200), rand.Next(5, 200)), new Point(rand.Next(5, 100), rand.Next(5, 100)) };
return points;
}
private string GetRandomText()
{
StringBuilder randomText = new StringBuilder();
if (Session["Code"] == null)
{
string alphabets = "abcdefghijklmnpqrstuvwxyz0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
Random r = new Random();
for (int j = 0; j <= 5; j++)
{
randomText.Append(alphabets[r.Next(alphabets.Length)]);
}
Session["Code"] = randomText.ToString();
}
return Session["Code"] as String;
}
}