您的位置:首页 > 编程语言 > ASP

ASP.NET 2.0 下的验证码控件

2007-11-02 14:01 387 查看
}

public static int Next(int min, int max)

{

//

}

}

RandomText 类

为了产生随机性很强的文字,我们使用RNG类产生随机数,从一个字符数组中取字符。我发现这是 CryptoPasswordGenerator中一个很有用的技术。

Code:

public static class RandomText

{

public static string Generate()

{

// Generate random text

string s = "";

char[] chars = "abcdefghijklmnopqrstuvw".ToCharArray() +

"xyzABCDEFGHIJKLMNOPQRSTUV".ToCharArray() +

"WXYZ0123456789".ToCharArray();

int index;

int lenght = RNG.Next(4, 6);

for (int i = 0; i < lenght; i++)

{

index = RNG.Next(chars.Length - 1);

s += chars[index].ToString();

}

return s;

}

}

CaptchaImage 类

该类是本控件的核心,它得到图像文字、尺寸、背景颜色,然后产生图像。

主要的方法是GenerateImage(),它根据我们提供的信息产生图片。
Code:

private void GenerateImage()

{

// Create a new 32-bit bitmap image.

Bitmap bitmap = new Bitmap(this.width, this.height,

PixelFormat.Format32bppArgb);

// Create a graphics object for drawing.

Graphics g = Graphics.FromImage(bitmap);

Rectangle rect = new Rectangle(0, 0, this.width, this.height);

g.SmoothingMode = SmoothingMode.AntiAlias;

// Fill background

using (SolidBrush b = new SolidBrush(bc))

{

g.FillRectangle(b, rect);

}

首先,声明Bitmap 和Graphics 对象,以及一个和Bitmap 对象一样尺寸的Rectangle 。然后使用SolidBrush填充背景。

现在,我们需要设置字体大小,因为,字体是从fonts字体集合中随机选择的。

Code:

// Set up the text font.

int emSize = (int)(this.width * 2 / text.Length);

FontFamily family = fonts[RNG.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(this.width, this.height);

while (emSize > 2 &&

(measured = g.MeasureString(text, font)).Width

> workingSize.Width || measured.Height

> workingSize.Height)

{

font.Dispose();

font = new Font(family, emSize -= 2);

}

下一步使用GraphicsPath 绘制文字。

Code:

GraphicsPath path = new GraphicsPath();

path.AddString(this.text, font.FontFamily,

(int)font.Style, font.Size, rect, format);

最重要的部分是给文字加颜色及扭曲文字:随机选择0-255之间的数字,然后使用这个RGB值给文字设置颜色。并且,需要检查颜色和背景色是否能够区分。

Code:

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

int bcR = Convert.ToInt32(bc.R);

int red = random.Next(256), green = random.Next(256), blue =

random.Next(256);

// This prevents font color from being near the bg color

while (red >= bcR && red - 20 < bcR ||

red < bcR && red + 20 > bcR)

{

red = random.Next(0, 255);

}

SolidBrush sBrush = new SolidBrush(Color.FromArgb(red, green, blue));

g.FillPath(sBrush, path);

最后,扭曲图像。

Code:

// Iterate over every pixel

double distort = random.Next(5, 20) * (random.Next(10) == 1 ? 1 : -1);

// Copy the image so that we're always using the original for

// source color

using (Bitmap copy = (Bitmap)bitmap.Clone())

{

for (int y = 0; y < height; 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 >= height)

newY = 0;

bitmap.SetPixel(x, y,

copy.GetPixel(newX, newY));

}

}

}

Captcha.ashx

这个HTTP 句柄得到需要创建验证码图像的信息,然后创建一个。注意:它接收到的是加密后的文字,并使用密钥解密。

Code:

public class Captcha : IHttpHandler

{

public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "image/jpeg";

context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

context.Response.BufferOutput = false;

// Get text

string s = "No Text";

if (context.Request.QueryString["c"] != null &&

context.Request.QueryString["c"] != "")

{

string enc = context.Request.QueryString["c"].ToString();

// space was replaced with + to prevent error

enc = enc.Replace(" ", "+");

try

{

s = Encryptor.Decrypt(enc, "srgerg$%^bg",

Convert.FromBase64String("srfjuoxp"));

}

catch { }

}

// Get dimensions

int w = 120;

int h = 50;

// Width

if (context.Request.QueryString["w"] != null &&

context.Request.QueryString["w"] != "")

{

try

{

w = Convert.ToInt32(context.Request.QueryString["w"]);

}

catch { }

}

// Height

if (context.Request.QueryString["h"] != null &&

context.Request.QueryString["h"] != "")

{

try

{

h = Convert.ToInt32(context.Request.QueryString["h"]);

}

catch { }

}

// Color

Color Bc = Color.White;

if (context.Request.QueryString["bc"] != null &&

context.Request.QueryString["bc"] != "")

{

try

{

string bc = context.Request.QueryString["bc"].

ToString().Insert(0, "#");

Bc = ColorTranslator.FromHtml(bc);

}

catch { }

}

// Generate image

CaptchaImage ci = new CaptchaImage(s, Bc, w, h);

// Return

ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

// Dispose

ci.Dispose();

}

public bool IsReusable

{

get

{

return true;

}

}

}

这里有两点需要说明:

1. 因为,在URL中,'+'意思是空格,所以我们把空格都替换成+

2. #在URL中会产生问题,我们不发送颜色值中的#,例如:当颜色是#ffffff的时候,我们只发送ffffff,在处理的时候自动加上#。

[总结]

在文章的源代码中,除了控件的源代码,你还可以见到一个使用这个控件的例子。
http://files.cnblogs.com/jueban/QQ(1).rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐