ASP验证码代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
blackbrush.Dispose();//释放画笔;
g.Dispose();//释放绘图对象;
image.Dispose();//释放图形对象;
}
protected void Page_Load(object sender, EventArgs e)
{
draw();
}
}
{
//
// CODEGEN:该调用是ASP.NET Web窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
/// </summary>
private void InitializeComponent()
public void CreateImages(string code)
{
//创建一个Bitmap新实例
Bitmap image = new Bitmap(60, 20);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww = new WebColorConverter();
string[] strArray = so.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < codeLength; i++)
{
//Random.Next(minValue,maxValue);
//一个大于或等于minValue且小于maxValue的32位带符号整数,即:返回的值范围包括minValue但不包括maxValue。如果
//minValue等于maxValue,则返回minValue。
code += strArray[rand.Next(0, strArray.Length)];
}
return code;
}
/// <summary>
///产生验证图片
/// </summary>
/// <param name="code"></param>
{
//产生4位验证码
string CheckCode = CreateCode(4);
//用于验证
Session["code"] = CheckCode;
CreateImages(CheckCode);
}
/// <summary>
///产生验证码
/// </summary>
/// <param name="codeLength"></param>
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);
using System.Drawing;
public class Rand
{
public string rand;
Random ran = new Random(DateTime.Now.Millisecond);//加个随机数种子
public Rand()
{
for (int i = 0; i < 4; i++)
}
g.DrawString(R.rand, font, blackbrush, 20, 10);
Response.ContentType = "image/jpeg";
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//输出图片;
using System.Web.UI.WebControls.WebParts;
using System.XML.Linq;
using System.Drawing;
public partial class CreateImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
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);
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
以上是CreateImage.aspx文件的后台代码,完成上述方法后,在页面里调用方法,只需要添加<img src="CreateImage.aspx" align="middle" />,即可显示生成的验证码图片,每次刷新都会随机产生不同的验证码,如下:
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);//创建画图对象;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateImage.aspx.cs" Inherits="CreateImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
在ASP页面中生成图片验证码,需要用到System.Drawing命名空间下的很多类,首先我们需要新建一个CreateImage.aspx页面,在后台代码中定义用于生成验证码图片的方法,如下:
using System;
using System.Collections;
using System.Configuration;
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);
//清楚整个绘图面,并以制定颜色填充
g.Clear((Color)ww.ConvertFromString("#FAE264"));
Random rand = new Random();
//画图片的背景噪音线
for (int i = 0; i < 12; i++)
{
int x1 = rand.Next(image.Width);
}
//新建字体
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Itali源自文库);
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)
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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;
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.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(DateTime.Now.Millisecond);//加个随机数种子
/// <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";
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.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);
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CheckImage</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="CreateImage.aspx" align="middle" />
相关文档
最新文档