图形验证码的生成(数字和英文大小写)和提交验证
最初接触时,感觉很好玩,然后就自己研究了下,做了个demo,然后整理下,下次可以直接使用啦,英文大小写,点击可以切换
上代码了。。。。
页面代码:
<img id="Img" src="/Login/GetCheckCode" />
这个是页面JQuery点击更换方法
$("#Img").click(function () {
//this.src = "/Login/GetCheckCode?time="+(new Date()).getTime(); //这个也可以的
//$("#Img").attr("src", "/Login/GetCheckCode?"+Math.random());
$("#Img").attr("src", "/Login/GetCheckCode?time=" + (new Date()).getTime());
注意:src后面加个time的时间参数,是为了再次加载验证码,否则加载不了。
网上的解释:服务器端编写好生成验证码的方法,由于客户端浏览器会缓存URL相同的资源,可以使用随机数/或者时间来重新请求:
});
后台代码:
public ActionResult GetCheckCode()
{
string code = GenerateCheckCodes(4);
Session["ValidateCode"] = code;
byte[] bytes = CreateCheckCodeImage(code);
return File(bytes, @"image/jpeg");
}
private string GenerateCheckCodes(int iCount)
{
char[] oCharacter = {'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'
};
//int number;
string checkCode = String.Empty;
int iSeed = DateTime.Now.Millisecond;
System.Random random = new Random(iSeed);
for (int i = 0; i < iCount; i++)
{
checkCode += oCharacter[random.Next(oCharacter.Length)];
//纯数字
//number = random.Next(10);
//number = oCharacter[random.Next(oCharacter.Length)];
//checkCode += number.ToString();
}
return checkCode;
}
private byte[] CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return null;
}
int iWordWidth = 20;
int iImageWidth = checkCode.Length * iWordWidth;
Bitmap image = new Bitmap(iImageWidth, 30);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音点
for (int i = 0; i < 20; 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);
}
//画图片的背景噪音线
for (int i = 0; i < 2; 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", 16, (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("楷体", 16, (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("宋体", 16, (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("黑体", 16, (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);
}
}
////画图片的前景噪音点 ---有无这段代码 貌似没啥变化
for (int i = 0; i < 8; 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);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
//Response.ClearContent();
//Response.ContentType = "image/jpeg";
//Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
好了,就到这里了,有问题或者不正确的可以指正,我每天都会看一次。谢谢,希望对你有帮助。
说一下,这个代码的格式化 ,太麻烦了,每次都要一行一行的tab键。