asp.net彩色汉字验证码

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.IO;

using System.Text;

using System.Drawing;

public partial class ValidateCode : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

this.CreateImage(CreatValidateCode());

}

private string CreatValidateCode()

{

Encoding gb = Encoding.GetEncoding("gb2312");

string[] storecode= new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

Random rnd=new Random();

object[] codes=new object[4];

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

{

int rcode1=rnd.Next(11,14);

string str_rcode1=storecode[rcode1].Trim();

rnd=new Random(rcode1*unchecked((int)DateTime.Now.Ticks)+i);

int rcode2;

if (rcode1==13)

{

rcode2=rnd.Next(0,7);

}

else

{

rcode2=rnd.Next(0,16);

}

string str_rcode2=storecode[rcode2].Trim();

rnd=new Random(rcode2*unchecked((int)DateTime.Now.Ticks)+i);

int rcode3=rnd.Next(10,16);

string str_rcode3=storecode[rcode3].Trim();

rnd=new Random(rcode3*unchecked((int)DateTime.Now.Ticks)+i);

int rcode4;

if (rcode3==10)

{

rcode4=rnd.Next(1,16);

}

else if (rcode3==15)

{

rcode4=rnd.Next(0,15);

}

else

{

rcode4=rnd.Next(0,16);

}

string str_rcode4=storecode[rcode4].Trim();

byte byte1=Convert.ToByte(str_rcode1 + str_rcode2,16);

byte byte2=Convert.ToByte(str_rcode3 + str_rcode4,16);

byte[] storebytes=new byte[]{byte1,byte2};

codes.SetValue(storebytes,i);

}

object[] showbytes = codes;

string strcode1 = gb.GetString((byte[])Convert.ChangeType(showbytes[0], typeof(byte[])));

string strcode2 = gb.GetString((byte[])Convert.ChangeType(showbytes[1], typeof(byte[])));

string strcode3 = gb.GetString((byte[])Convert.ChangeType(showbytes[2], typeof(byte[])));

string strcode4 = gb.GetString((byte[])Convert.ChangeType(showbytes[3], typeof(byte[])));

string checkValidateCode = strcode1 + strcode2 + strcode3 + strcode4;

Response.Cookies.Add(new HttpCookie("ValidateCode", checkValidateCode));

return checkValidateCode;

}

private void CreateImage(string checkValidateCode)

{

if (checkValidateCode == null || checkValidateCode.Trim() == String.Empty)

return;

System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkValidateCode.Length * 45.0)),80);

Graphics g = Graphics.FromImage(image);

try

{

Random random = new Random();

g.Clear(Color.White);

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

{

int x1 = random.Next(image.Width);

int x2 = random.Next(image.Width);

int y1 = random.Next(image.Height);

int y2 = random.Next(image.Height);

g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

}

string[] pickfonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };

Color[] pickcolors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };

for (int i = 0; i < checkValidateCode.Length; i++)

{

int pickindex = random.Next(7);

int fontindex = random.Next(4);

Font font_f = new System.Drawing.Font(pickfonts[fontindex], 30, System.Drawing.FontStyle.Bold);

Brush solid_b = new System.Drawing.SolidBrush(pickcolors[pickindex]);

int j = 30;

if ((i + 1) % 2 == 0)

{

j =2;

}

g.DrawString(checkValidateCode.Substring(i, 1), font_f, solid_b, 3 + (i * 40), j);

}

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

{

int x = random.Next(image.Width);

int y = random.Next(image.Height);

image.SetPixel(x, y, Color.FromArgb(random.Next()));

}

g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

System.IO.MemoryStream ms = new System.IO.MemoryStream();

image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

Response.ClearContent();

Response.ContentType = "image/Bmp";

Response.BinaryWrite(ms.ToArray());

}

finally

{

g.Dispose();

image.Dispose();

}

}

}