ASP.NET中的无刷新验证码的开发(完整代码)

时间:2021-05-28

复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://)
image = TwistImage(image, true, 4, 4);
return image;
}
#endregion
#region 将创建好的图片输出到页面
public void CreateImageOnPage(string code, HttpContext context)
{
Response.BufferOutput = true; //特别注意
Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
Response.AppendHeader("Pragma", "No-Cache"); //特别注意
MemoryStream ms = new MemoryStream();
Bitmap image = this.CreateImageCode(code);
image.Save(ms, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/JPEG";
Response.BinaryWrite(ms.ToArray());
Response.End();
ms.Close();
ms = null;
image.Dispose();
image = null;
}
#endregion
#region 生成随机字符码
public string CreateVerifyCode(int codeLen)
{
if (codeLen == 0)
{
codeLen = Length;
}
string[] arr = CodeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
return code;
}
public string CreateVerifyCode()
{
return CreateVerifyCode(0);
}
#endregion
#region 另一种验证码样式 GenerateVerifyImage(int length)
/// <summary>
/// 将创建好的图片输出到页面
/// </summary>
public void GenerateVerifyImage(int nLen)
{
string validateCode = "";//生成的验证码
int nBmpWidth = GetImagewidth(nLen);
int nBmpHeight = GetImageHeight();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth, nBmpHeight);
//对图像进行弯曲
TwistImage(bmp, true, 12, 2);

// 1. 生成随机背景颜色
int nRed, nGreen, nBlue; // 背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255) % 128 + 128;
nGreen = rd.Next(255) % 128 + 128;
nBlue = rd.Next(255) % 128 + 128;
// 2. 填充位图背景
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue))
, 0
, 0
, nBmpWidth
, nBmpHeight);

// 3. 绘制干扰线条,采用比背景略深一些的颜色
int nLines = 3;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
for (int a = 0; a < nLines; a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen, x1, y1, x2, y2);
}
// 采用的字符集,可以随即拓展,并可以控制字符出现的几率
string strCode = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 4. 循环取得字符,并绘制
for (int i = 0; i < nLen; i++)
{
int x = (i * 13 + rd.Next(3));
int y = rd.Next(4) + 1;
// 确定字体
System.Drawing.Font font = new System.Drawing.Font("Courier New",//文字字体类型
12 + rd.Next() % 4,//文字字体大小
System.Drawing.FontStyle.Bold);//文字字体样式
char c = strCode[rd.Next(strCode.Length)]; // 随机获取字符
validateCode += c.ToString();
// 绘制字符
graph.DrawString(c.ToString(),
font,
new SolidBrush(System.Drawing.Color.FromArgb(nRed - 60 + y * 3, nGreen - 60 + y * 3, nBlue - 40 + y * 3)),
x,
y);
}
Session["ValidateCode"] = validateCode;
//对图像进行弯曲
TwistImage(bmp, true, 4, 4);
Response.BufferOutput = true; //特别注意
Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
Response.AppendHeader("Pragma", "No-Cache"); //特别注意
// 5. 输出字节流
MemoryStream bstream = new MemoryStream();
bmp.Save(bstream, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/JPEG";
Response.BinaryWrite(bstream.ToArray());
Response.End();
bstream.Close();
bstream = null;
bmp.Dispose();
bmp = null;
graph.Dispose();
}
///<summary>
///得到验证码图片的宽度
///</summary>
///<paramname="validateNumLength">验证码的长度</param>
///<returns></returns>
public static int GetImagewidth(int validateNumLength)
{
return (int)(13 * validateNumLength + 5);
}
///<summary>
///得到验证码的高度
///</summary>
///<returns></returns>
public static int GetImageHeight()
{
return 25;
}
#endregion
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章