当前位置:文档之家› ASP验证码代码

ASP验证码代码

}
g.DrawString(R.rand, font, blackbrush, 20, 10);
Response.ContentType = "image/jpeg";
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//输出图片;
public int x,y,p,q;
public int r, g, b;
protected void draw()
{
Rand R = new Rand();
Session["image"] = R.rand;
Bitmap image = new Bitmap(100, 50);//创建画布;
Graphics g = Graphics.FromImage(image);//创建画图对象;
g.Clear(Color.Black);
SolidBrush blackbrush = new SolidBrush(Color.White);
Font font = new Font("Verdana", 14);
for (int i = 0; i < 100; i++)
{
this.x = ran.Next(0, 100);
<html xmlns="/1999/xhtml">
<head runat="server">
<title>CheckImage</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="CreateImage.aspx" align="middle" />
this.y = ran.Next(0, 50);
this.p = ran.Next(0, 100);
this.q = ran.Next(0, 50);
this.r = ran.Next(0, 256);
this.g = ran.Next(0, 256);
this.b = ran.Next(0, 256);
/// <returns></returns>
public string CreateCode(int codeLength)
{
string so = "1,2,3,4,5,6,7,8,9,0,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";
blackbrush.Dispose();//释放画笔;
g.Dispose();//释放绘图对象;
image.Dispose();//释放图形对象;
}
protected void Page_Load(object sender, EventArgs e)
{
draw();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateImage.aspx.cs" Inherits="CreateImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HTMLControls;
using System.Web.UI.WebControls;
int x2 = rand.Next(image.Height);
int y1 = rand.Next(image.Width);
int y2 = rand.Next(image.Height);
g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
g.DrawString(code, font, brush, 0, 0);
//画图片的前景噪音
for (int i = 0; i < 10; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
image.SetPixel(x, y, Color.White);
}
//新建字体
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
#region Web窗体设计器生成的代码
override protected void OnInit(EventArgs e)
public void CreateImages(string code)
{
//创建一个Bitmap新实例
Bitmap image = new Bitmap(60, 20);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww = new WebColorConverter();
</div>
</form>
</body>
</html>
using System;
usinSystem.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
{
this.rand += ran.Next(0, 16).ToString("x1");
}
HttpContext.Current.Session.Add("code",this.rand);
}
}
public partial class image : System.Web.UI.Page
{
Random ran = new Random(lisecond);//加个随机数种子
在ASP页面中生成图片验证码,需要用到System.Drawing命名空间下的很多类,首先我们需要新建一个CreateImage.aspx页面,在后台代码中定义用于生成验证码图片的方法,如下:
using System;
using System.Collections;
using System.Configuration;
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Pen pen = new Pen(Color.FromArgb(this.r, this.g, this.b));
g.DrawLine(pen, this.x, this.y, this.x, this.y + 1);
//g.DrawLine(pen, this.x, this.y, this.x + this.p, this.y + this.q);
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
以上是CreateImage.aspx文件的后台代码,完成上述方法后,在页面里调用方法,只需要添加<img src="CreateImage.aspx" align="middle" />,即可显示生成的验证码图片,每次刷新都会随机产生不同的验证码,如下:
{
//
// CODEGEN:该调用是 Web窗体设计器所必需的。
相关主题