Font font = new System Drawing Font( Arial (System Drawing FontStyle Bold | System Drawing FontStyle Italic)) System Drawing Drawing D LinearGradientBrush brush = new System Drawing Drawing D LinearGradientBrush(new Rectangle( image Width image Height) Color Blue Color DarkRed f true) g DrawString(checkCode font brush ) //画图片的前景噪音点 for(int i= i<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 Silver) image Width image Height ) 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()) } finally { g Dispose() image Dispose() } } } 在你要显示验证码的窗体中添加一个image控件 并命名为imgCheckCode 用于显示验证码 再添加一个lable 并命名为lblMessage 用于显示错误信息 然后在该窗体的Page_Load中添加 this imgCheckCode ImageUrl = checkCode aspx 在登录页面的登录按钮的处理事件中使用以下代码判断验证码 private void btnLogin_Click(object sender System Web UI ImageClickEventArgs e) { if(Request Cookies[ checkCode ] == null) { lblMessage Text = 您的浏览器设置已被禁用 Cookies 您必须设置浏览器允许使用 Cookies 选项后才能使用本系统 lblMessage Visible = true return } if(String Compare(Request Cookies[ checkCode ] Value txtCheckCode Text true) != ) { lblMessage Text = 验证码错误 请输入正确的验证码 lblMessage Visible = true return } } 这样就能实现你所要的验证码验证了 lishixinzhi/Article/program/net/201311/13119
第一步:添加一个页面,页面代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckCode.aspx.cs" Inherits="CheckCode" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
第二步:添加页面的cs代码:
using System
using System.Collections
using System.Configuration
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
using System.Web.UI.WebControls.WebParts
using System.Xml.Linq
using System.Drawing
//这是一个生成验证码的网页
public partial class CheckCode : System.Web.UI.Page
{
private string GenerateCheckCode()
{
int num
char code
string checkCode = string.Empty
Random random = new Random()
for (int i = 0i <6i++)//循环次数决定验证码的位数
{
num = random.Next()
if (num % 2 == 0)
{
code = (char)('0' + (char)(num % 10))
}
else
{
code = (char)('A' + (char)(num % 26))
}
checkCode += code.ToString()
}
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode))
return checkCode
}
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImg(GenerateCheckCode())
}
private void CreateCheckCodeImg(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return
Bitmap img = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22)
Graphics g = Graphics.FromImage(img)
try
{
Random random = new Random()
g.Clear(Color.White)
//画图片的背景线
for (int i = 0i <2i++)
{
int x1 = random.Next(img.Width)
int x2 = random.Next(img.Width)
int y1 = random.Next(img.Width)
int y2 = random.Next(img.Width)
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2)
}
//画出指定的字符
Font font = new Font("Arial", 12, (FontStyle.Bold))
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Red, 1.2f, true)
g.DrawString(checkCode, font, brush, 2, 2)
//画图片的前景噪点
for (int i = 0i <100i++)
{
int x = random.Next(img.Width)
int y = random.Next(img.Height)
img.SetPixel(x, y, Color.FromArgb(random.Next()))
}
g.DrawRectangle(new Pen(Color.Silver),0,0,img.Width-1,img.Height-1)
System.IO.MemoryStream ms=new System.IO.MemoryStream()
img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif)
Response.ClearContent()
Response.ContentType="image/Gif"
Response.BinaryWrite(ms.ToArray())
}
finally
{
g.Dispose()
img.Dispose()
}
}
}
第三步:在需要验证码的网页引用产生验证码的网页:
在你需要调用验证码的网页中添加一个<img>标签,将img标签的src设置成刚刚新添加的那个aspx!比如<img alt="验证码" src="CkeckCode.aspx"></img>
下面是刷新功能:
你可以把img标签和一个linkbutton一起加入到updatepanel中,然后生成一个linkbutton的click事件,不用在里面写任何代码的,就可以达到刷新验证码的效果了.
最后一步:验证验证码:
在验证页面里添加以下代码:
HttpCookie cookie = Request.Cookies["CheckCode"]
if (cookie.Value == tbYanZM.Text)
{
Response.Write("...验证码正确")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)