public static string RandomNum(int n)
{
string strchar = "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,V,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[] VcArray = strchar.Split(',')
string VNum = ""
int temp = -1
Random rand = new Random()
for (int i = 1i <n + 1i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks))
}
int t = rand.Next(61)
if (temp != -1 &&temp == t)
{
return RandomNum(n)
}
temp = t
VNum += VcArray[t]
}
return VNum
}
#endregion
#region 验证码图片
public static void CheckCodes(string M_CheckCode)
{
Random rand = new Random()
int iwidth = (int)(M_CheckCode.Length * 15)
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22)
Graphics g = Graphics.FromImage(image)
g.Clear(Color.White)
//画图片的背景噪音线20条
for (int i = 0i <20i++)
{
int x1 = rand.Next(image.Width)
int x2 = rand.Next(image.Width)
int y1 = rand.Next(image.Height)
int y2 = rand.Next(image.Height)
g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2)
}
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen }
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "新宋体" }
//随机输出噪点
for (int i = 0i <50i++)
{
int x = rand.Next(image.Width)
int y = rand.Next(image.Height)
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1)
}
//输出不同字体和颜色的验证码字符
for (int i = 0i <M_CheckCode.Lengthi++)
{
int cindex = rand.Next(7)
int findex = rand.Next(6)
Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold)
Brush b = new System.Drawing.SolidBrush(c[cindex])
int ii = 4
if ((i + 1) % 2 == 0)
{
ii = 2
}
g.DrawString(M_CheckCode.Substring(i, 1), f, b, 3 + (i * 12), ii)
}
//画一个边框
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1)
//输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
HttpContext.Current.Response.ClearContent()
//Response.ClearContent()
HttpContext.Current.Response.ContentType = "image/gif"
HttpContext.Current.Response.BinaryWrite(ms.ToArray())
g.Dispose()
image.Dispose()
}
#endregion
通过javascript获得。1、验证码一般是由后台生成的图片,如果用HTML制作的话很容易被软件获取,如果要用HTML的话就用javascript生成随机数。
2、在写一个英文字母的数组,用随机数来取下标吧,如果只用数字验证码的话随机数就可以,不过验证码不建议用javascript制作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)