Asp.Net生成图片验证码(C/S框架网开源)
Asp.Net生成图片验证码(C/S框架网开源)
using System;
using System.Data;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Drawing2D;
using System.Threading;
/*
* 版权:C/S框架网 www.csframework.com
*
* 生成图片验证码业务逻辑
*/
namespace CSFramework.BLL
{
/// <summary>
/// 生成图片验证码
/// </summary>
public class CGenValidateCode
{
#region 随机字符验证码
private Page _page = null;
public CGenValidateCode(Page page)
{
_page = page;
}
/// <summary>
/// 产生验证码
/// </summary>
/// <returns></returns>
private string CreateCode(int codeLength)
{
//去除字母与数字相近的数字或字母
string chars = "2,3,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
"a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
string[] strArr = chars.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < codeLength; i )
{
code = strArr[rand.Next(0, strArr.Length)];
Thread.Sleep(5);
}
return code;
}
/// <summary>
/// 输出验证图片
/// </summary>
/// <param name="code"></param>
private void CreateImage(string code)
{
Bitmap image = new Bitmap(75, 25); //验证码图片模块
Graphics g = Graphics.FromImage(image);
WebColorConverter ww = new WebColorConverter();
g.Clear((Color)ww.ConvertFromString("White")); //底色
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 6; i )
{
int x1 = random.Next(image.Width);
Thread.Sleep(5);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
Thread.Sleep(5);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Blue), x1, y1, x2, y2);
}
//验证码字体
Font font = new Font("arial", 14, FontStyle.Bold | FontStyle.Italic);
//画笔对象
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.Gray, 1.2f, true);
g.DrawString(code, font, brush, 0, 0);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
_page.Response.ClearContent();
_page.Response.ContentType = "image/jpeg"; //HTTP MIME TYPE 设为图片输出
_page.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
/// <summary>
/// 生成新的验证码
/// </summary>
public void Generate()
{
string checkCode = this.CreateCode(5); //生成新的验证码
_page.Session["csframework_code"] = checkCode;
this.CreateImage(checkCode);
}
/// <summary>
/// 检验用户输入的验证码
/// </summary>
/// <param name="validatePage">要验证的页面</param>
/// <param name="input">用户输入的验证码</param>
/// <returns></returns>
public static bool ValidateLastCode(Page validatePage, string input)
{
//将对象转换为字符串
string lastCode = Convert.ToString(validatePage.Session["csframework_code"]);
return (input.ToUpper() == lastCode.ToUpper());
}
#endregion
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
using System.Data;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Drawing2D;
using System.Threading;
/*
* 版权:C/S框架网 www.csframework.com
*
* 生成图片验证码业务逻辑
*/
namespace CSFramework.BLL
{
/// <summary>
/// 生成图片验证码
/// </summary>
public class CGenValidateCode
{
#region 随机字符验证码
private Page _page = null;
public CGenValidateCode(Page page)
{
_page = page;
}
/// <summary>
/// 产生验证码
/// </summary>
/// <returns></returns>
private string CreateCode(int codeLength)
{
//去除字母与数字相近的数字或字母
string chars = "2,3,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
"a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
string[] strArr = chars.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < codeLength; i )
{
code = strArr[rand.Next(0, strArr.Length)];
Thread.Sleep(5);
}
return code;
}
/// <summary>
/// 输出验证图片
/// </summary>
/// <param name="code"></param>
private void CreateImage(string code)
{
Bitmap image = new Bitmap(75, 25); //验证码图片模块
Graphics g = Graphics.FromImage(image);
WebColorConverter ww = new WebColorConverter();
g.Clear((Color)ww.ConvertFromString("White")); //底色
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 6; i )
{
int x1 = random.Next(image.Width);
Thread.Sleep(5);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
Thread.Sleep(5);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Blue), x1, y1, x2, y2);
}
//验证码字体
Font font = new Font("arial", 14, FontStyle.Bold | FontStyle.Italic);
//画笔对象
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.Gray, 1.2f, true);
g.DrawString(code, font, brush, 0, 0);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
_page.Response.ClearContent();
_page.Response.ContentType = "image/jpeg"; //HTTP MIME TYPE 设为图片输出
_page.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
/// <summary>
/// 生成新的验证码
/// </summary>
public void Generate()
{
string checkCode = this.CreateCode(5); //生成新的验证码
_page.Session["csframework_code"] = checkCode;
this.CreateImage(checkCode);
}
/// <summary>
/// 检验用户输入的验证码
/// </summary>
/// <param name="validatePage">要验证的页面</param>
/// <param name="input">用户输入的验证码</param>
/// <returns></returns>
public static bool ValidateLastCode(Page validatePage, string input)
{
//将对象转换为字符串
string lastCode = Convert.ToString(validatePage.Session["csframework_code"]);
return (input.ToUpper() == lastCode.ToUpper());
}
#endregion
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
网页中使用:
<td class="td2">
<span style="color: Gray;">(不区分大小写)</span>
<a href="#" onclick="document.getElementById('vcode').src='http://www.csframework.com/do-gen-code.aspx?temp='+ (new Date().getTime().toString(36));return false;">重新生成</a>
</td>
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc
C/S框架网