标签:
在做登录时通常是要带验证码,在aspx页面调用:
<input type="text" id="txtCode" style="width: 35px;height: 22px; padding-top: 3px;" maxlength="4" />
<img id="logingImg" align="absmiddle" onclick="GetSessionImg(this);" src="*/CheckCode.aspx"
style="width: 50px; cursor: pointer; height: 22px;" />
<a style="color: #528311; cursor: pointer; font-size: 12px;" onclick="GetSessionImg($(‘#logingImg‘));">换一张</a>
<script type="text/javascript">
//切换调用
function GetSessionImg(objthis) {
$("#txtCode").val("");
$(objthis).attr("src", "*/load_img/ajax-loader_05.gif");//加载缩略图
$(objthis).attr("src", "*/CheckCode.aspx?id=" + Math.random());
$("#txtCode").focus();
}
</script>
在CheckCode.aspx后置代码:
引用命名空间:using System.Drawing;
//加载调用
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
}
/// <summary>
/// 生成4位数的验证码
/// </summary>
private string GenerateCheckCode()
{
int number;
char code;
string ChkCode= String.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
{ //随机生成
number = random.Next();
if (number % 2 == 0)
code = (char)(‘0‘ + (char)(number % 10));//除以10取余数(随机数的各位如876的话就是6)00-09并强制转换
else
code = (char)(‘a‘ + (char)number % 26);//除以26取余数得到的结果数再以a开头往后数就是所得的值如number=28,即code=c
ChkCode+= code.ToString();
}
if (checkCode.Trim().Equals("") || checkCode.Trim().Length != 4)
{
GenerateCheckCode();//不够四位进行生成
}
else
{
Session["User_code"] =ChkCode;//存入会话用于验证登录是否匹配
}
return ChkCode;
}
/// <summary>
/// 输出验证码
/// </summary>
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
System.Drawing.Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); //返回大于或等于指定数字的最小整数。
Graphics g = Graphics.FromImage(image);//创建图像对象
try {
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 25; 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.GreenYellow), x1, y1, x2, y2); // 绘制一条连接由坐标对指定的两个点的线条
}
//字体
Font font = new System.Drawing.Font("Verdana", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
//笔刷
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle
(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 80; 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.Bisque), 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());//将一个二进制字符串写入 HTTP 输出流
} catch (Exception ex) {
Console.Write(ex.Message);
} finally {
//关闭或释放由实现此接口的类的实例保持的文件、流和句柄等非托管资源
g.Dispose();
image.Dispose();
}
}
标签:
原文地址:http://www.cnblogs.com/professional-NET/p/4678555.html