using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Drawing;
namespace
Form
{
public
partial
class
VerifyCode : System.Web.UI.Page
{
public
static
string
HZ;
/// <summary>
/// 验证码的最大长度
/// </summary>
public
int
MaxLength
{
get
{
return
10; }
}
/// <summary>
/// 验证码的最小长度
/// </summary>
public
int
MinLength
{
get
{
return
1; }
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
string
[] str = CreateValidateNumber(4);
string
strcode =
string
.Empty;
for
(
int
i = 0; i < str.Length; i++)
{
strcode += str[i];
}
CreateCheckCodeImage(str);
HZ = strcode;
Response.Write(HZ);
Session[
"ValidateCode"
] = HZ;
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns>验证码</returns>
public
string
[] CreateValidateNumber(
int
length)
{
string
Vchar =
"1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p"
+
",q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q"
+
",R,S,T,U,V,W,X,Y,Z"
;
string
[] VcArray = Vchar.Split(
new
Char[] {
','
});
string
[] num =
new
string
[length];
int
temp = -1;
Random rand =
new
Random();
for
(
int
i = 1; i < length + 1; i++)
{
if
(temp != -1)
{
rand =
new
Random(i * temp *
unchecked
((
int
)DateTime.Now.Ticks));
}
int
t = rand.Next(VcArray.Length-1);
if
(temp != -1 && temp == t)
{
return
CreateValidateNumber(length);
}
temp = t;
num[i - 1] = VcArray[t];
}
return
num;
}
private
void
CreateCheckCodeImage(
string
[] checkCode)
{
if
(checkCode ==
null
|| checkCode.Length <= 0)
return
;
System.Drawing.Bitmap image =
new
System.Drawing.Bitmap((
int
)Math.Ceiling((checkCode.Length * 32.5)), 60);
System.Drawing.Graphics g = Graphics.FromImage(image);
try
{
Random random =
new
Random();
g.Clear(Color.White);
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
for
(
int
i = 0; i < 25; i++)
{
int
cindex = random.Next(7);
int
findex = random.Next(5);
int
x1 = random.Next(image.Width);
int
x2 = random.Next(image.Width);
int
y1 = random.Next(image.Height);
int
y2 = random.Next(image.Height);
}
string
[] f = {
"Verdana"
,
"Microsoft Sans Serif"
,
"Comic Sans MS"
,
"Arial"
,
"宋体"
};
for
(
int
k = 0; k <= checkCode.Length - 1; k++)
{
int
cindex = random.Next(7);
int
findex = random.Next(5);
Font drawFont =
new
Font(f[findex], 26, (System.Drawing.FontStyle.Bold));
SolidBrush drawBrush =
new
SolidBrush(c[cindex]);
float
x = 5.0F;
float
y = 0.0F;
float
width = 42.0F;
float
height = 48.0F;
int
sjx = random.Next(10);
int
sjy = random.Next(image.Height - (
int
)height);
RectangleF drawRect =
new
RectangleF(x + sjx + (k * 25), y + sjy, width, height);
StringFormat drawFormat =
new
StringFormat();
drawFormat.Alignment = StringAlignment.Center;
g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat);
}
for
(
int
i = 0; i < 500; i++)
{
int
x = random.Next(image.Width);
int
y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
int
cindex1 = random.Next(7);
g.DrawRectangle(
new
Pen(c[cindex1]), 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.ContentType =
"image/Gif"
;
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}