作者:阿芙2011 | 来源:互联网 | 2022-09-25 22:28
这篇文章主要为大家详细介绍了一个简单的ASP.NET验证码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了ASP.NET验证码的具体代码,供大家参考,具体内容如下
我主要是看到干扰线了,一个验证码里面要是没有干扰线什么的,至少得在噪点和随机码的排版上下工夫:
///
/// 验证码生成类
///
public class verify_code : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
int codeW = 80;
int codeH = 22;
int fOntSize= 16;
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] fOnt= { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '0', '1', '2', '3', '4', '5', '6', '8', '9' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i <4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//写入Session
context.Session["sys_verify_code"] = chkCode;
//创建画布
Bitmap bmp = new Bitmap(codeW, codeH);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i <4; i++)
{
int x1 = rnd.Next(codeW);
int y1 = rnd.Next(codeH);
int x2 = rnd.Next(codeW);
int y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i 基本验证生成代码demo:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string tmp = RndNum(4);
HttpCOOKIE a = new HttpCOOKIE("ImageV", tmp);
Response.COOKIEs.Add(a);
this.ValidateCode(tmp);
}
private void ValidateCode(string VNum)
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
int gheight = VNum.Length * 12;
Img = new Bitmap(gheight, 25);
g = Graphics.FromImage(Img);
//背景颜色
g.Clear(Color.White);
//文字字体
Font f = new Font("Arial Black", 10);
//文字颜色
SolidBrush s = new SolidBrush(Color.Black);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, ImageFormat.Jpeg);
Response.ClearContent();
Response.COntentType= "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private string RndNum(int VcodeNum)
{
string Vchar = "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";
string[] VcArray = Vchar.Split(new Char[] { &#39;,&#39; });
string VNum = "";
int temp = -1;
Random rand = new Random();
for (int i = 1; i 以上就是ASP.NET验证码制作的详细内容,更多请关注其它相关文章!