验证码的制作
www.diybl.com 时间 : 2008-03-24 作者:佚名 编辑:本站 点击: [ 评论 ]
C#用的是GDI+,还没深入学习,下面代码是根据书上和网上的资料写的,仿GOOGLE的扭曲验证码
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{

String validateCode = GenerateCode();
System.Drawing.Bitmap image = new Bitmap((int)Math.Ceiling((validateCode.Length * 17.0)), 30);
Graphics g = Graphics.FromImage(image);
try
...{
Random random = new Random();
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 14,
(System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush;
for (int i = 0; i < validateCode.Length; i++)
...{
brush = new System.Drawing.Drawing2D.LinearGradientBrush
(new Rectangle(0, 0, image.Width, image.Height),
Color.FromArgb(random.Next(255), random.Next(255), random.Next(255))
, Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)),
1.2f, true);
g.DrawString(validateCode.Substring(i, 1), font, brush, 1 + 16 * i, 2);
}


g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image = TwistImage(image, true, 4, random.Next(5) + (double)random.Next(9) / 10.0 + (double)random.Next(9) / 100.0);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
...{
g.Dispose();
image.Dispose();
}
}
protected String GenerateCode()
...{
int number;
char code;
String validateCode = "";
Random random = new Random();
int length = random.Next(4, 7);
for (int i = 0; i < length; i++)
...{
number = random.Next();
if (number % 3 == 0)
code = (char)(''1'' + (char)(number % 9));
else if (number % 3 == 1)
code = (char)(''A'' + (char)(number % 26));
else
code = (char)(''a'' + (char)(number % 26));
validateCode += code.ToString();
}
Session["validateCode"] = validateCode;
return validateCode;
}
// <param name="srcBmp">图片路径</param> 

// <param name="bXDir">如果扭曲则选择为True</param>
// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
// <returns></returns>
public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
...{

System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);

graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
...{

for (int j = 0; j < destBmp.Height; j++)
...{

double dx = 0;
dx = bXDir ? (3.1415926 * 2 * (double)j) / dBaseAxisLen : (3.1415926 * 2 * (double)i) / dBaseAxisLen;

dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);

if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
...{

destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
return destBmp;
}
}

效果:

