怎样做一个C#的登陆验证码?
3个回答
推荐于2016-03-18
展开全部
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
private string CreateRandomCode(int codeCount)
{
// 函数功能:产生数字和字符混合的随机字符串
string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] allCharArray = allChar.ToCharArray();
string randomCode = "";
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
int r=rand.Next(61);
randomCode+=allCharArray.GetValue(r);
}
return randomCode;
}
private void CreateImage(string checkCode)
{
// 生成图象验证码函数
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.Azure);//字母白色
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Brown);//背景灰色
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
网上找的,感谢原作者
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
private string CreateRandomCode(int codeCount)
{
// 函数功能:产生数字和字符混合的随机字符串
string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] allCharArray = allChar.ToCharArray();
string randomCode = "";
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
int r=rand.Next(61);
randomCode+=allCharArray.GetValue(r);
}
return randomCode;
}
private void CreateImage(string checkCode)
{
// 生成图象验证码函数
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.Azure);//字母白色
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Brown);//背景灰色
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
网上找的,感谢原作者
展开全部
首先把 0-9 九答弯稿个数字 a-z 大小写字母添加到数组中 然后清孝 随闹蠢机数组序号 根据序号 获得 0-9 a-z 中的任何一个 如果需要生成5位的就随机五次 最后 组成字符串就是随机出现的验证码了 再加点 就是加上水印了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上很多登录验证码的源代码了,上百度上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询