来自:http://blog.csdn.net/hellojimmy520/article/details/7242630
protected void Page_Load(object sender, EventArgs e)
{
string gif = string.Empty;
switch (Request.QueryString["op"])
{
case "1": //汉字
gif = stxt(10);
Session["gif"] = stxt(4);
break;
case "2": //数字
gif = GetRandomint(4);
Session["gif"] = GetRandomint(4);
break;
case "3": //混合
gif = CreateRandomCode(4);
Session["gif"] = CreateRandomCode(4);
break;
default: //默认
gif = CreateRandomCode(4);
Session["gif"] = CreateRandomCode(4);
break;
}
CreateImage(Session["gif"].ToString());
}
/// <summary>
/// 产生一个随机数字
/// </summary>
/// <param name="codeCount">数字的位数</param>
/// <returns>数字字符串</returns>
private String GetRandomint(int codeCount)
{
Random random = new Random();
string min = "";
string max = "";
for (int i = 0; i < codeCount; i++)
{
min += "1";
max += "9";
}
return (random.Next(Convert.ToInt32(min), Convert.ToInt32(max)).ToString());
}
/// <summary>
/// 产生一个 随机中文汉字编码
///
/// 此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将
/// 四个字节数组存储在object数组中。
///
/// </summary>
/// <param name="strlength">需要产生的汉字个数 </param>
/// <returns></returns>
public static object[] CreateRegionCode(int strlength)
{
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
//定义一个object数组用来
object[] bytes = new object[strlength];
/**/
/*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
*/
for (int i = 0; i < strlength; i++)
{
//区位码第1位
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
//区位码第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i); //更换随机数发生器的种子避免产生重复值
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
//区位码第3位
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
//区位码第4位
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
//将两个字节变量存储在字节数组中
byte[] str_r = new byte[] { byte1, byte2 };
//将产生的一个汉字的字节数组放入object数组中
bytes.SetValue(str_r, i);
}
return bytes;
}
/// <summary>
/// 得到一个随机汉字字符串
/// </summary>
/// <param name="num">汉字的长度</param>
/// <returns></returns>
private string stxt(int num)
{
Encoding gb = Encoding.GetEncoding("gb2312");
//调用函数产生 随机中文汉字编码
object[] bytes = CreateRegionCode(num);
string strtxt = "";
//根据汉字编码的字节数组解码出中文汉字
for (int i = 0; i < num; i++)
{
strtxt += gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
}
return strtxt;
}
/// <summary>
/// 这个是使用字母,数字混合
/// </summary>
/// <param name="VcodeNum">验证码的长度</param>
/// <returns></returns>
private string CreateRandomCode(int codeCount)
{
string allChar =
"0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
/// <summary>
/// 将产生的验证码画成图
/// </summary>
/// <param name="checkCode">验证码字符串</param>
private void CreateImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
int iWordWidth = 25;
int iImageWidth = checkCode.Length * iWordWidth;
Bitmap image = new Bitmap(iImageWidth, 25);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的前景噪音点
for (int i = 0; i < 80; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的背景噪音线
/* for (int i = 0; i < 15; 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.Gray), x1, y1, x2, y2);
}
//画图片的背景噪音点
for (int i = 0; i < 50; 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.DrawRectangle(new Pen(Color.LightBlue,0), x1, y1, 1, 1);
}
//画图片的背景噪音线
for (int i = 0; i < 20; i++)
{
int x1 = 0;
int x2 = image.Width;
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
if (i == 0)
{
g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
}
}
*/
for (int i = 0; i < checkCode.Length; i++)
{
string Code = checkCode[i].ToString();
int xLeft = iWordWidth * (i);
random = new Random(xLeft);
int iSeed = DateTime.Now.Millisecond;
int iValue = random.Next(iSeed) % 4;
if (iValue == 0)
{
Font font = new Font("Arial", 12, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 1)
{
Font font = new System.Drawing.Font("楷体", 12, (FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 2)
{
Font font = new System.Drawing.Font("宋体", 12, (System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 3)
{
Font font = new System.Drawing.Font("黑体", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
}
//画图片的边框线
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.Gif);
Response.ClearContent();
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}