9月19日----asp.net简单的验证码生成
1.生成图片的页面
image.aspx
image.aspx.cs
2.调用的页面
yzm.aspx
image.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="image.aspx.cs" Inherits="image" %>
<!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>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="image.aspx.cs" Inherits="image" %>
<!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>
image.aspx.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(RunNum());
}
private string RunNum()
{
int Number;
char code;
string checkCode = String.Empty;
Random rm = new Random();
for (int i = 0; i < 4; i++)
{
Number = rm.Next();
if (Number % 2 == 0)
{
code = (char)('0' + (char)(Number % 10));
}
else
{
code = (char)('A' + (char)(Number % 26));
}
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("YZMcode", checkCode));
Session["yzmcode"] = checkCode;
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
Bitmap image = new Bitmap((int)checkCode.Length * 13, 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random rm_noise = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪声线
for (int i = 0; i < 25; i++)
{
int x1 = rm_noise.Next(image.Width);
int x2 = rm_noise.Next(image.Width);
int y1 = rm_noise.Next(image.Height);
int y2 = rm_noise.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
for (int i = 0; i < 100; i++)//画图片的噪声点
{
int x = rm_noise.Next(image.Width);
int y = rm_noise.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(rm_noise.Next()));
}
//在图片上写验证码
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkBlue, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
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, ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(RunNum());
}
private string RunNum()
{
int Number;
char code;
string checkCode = String.Empty;
Random rm = new Random();
for (int i = 0; i < 4; i++)
{
Number = rm.Next();
if (Number % 2 == 0)
{
code = (char)('0' + (char)(Number % 10));
}
else
{
code = (char)('A' + (char)(Number % 26));
}
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("YZMcode", checkCode));
Session["yzmcode"] = checkCode;
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
Bitmap image = new Bitmap((int)checkCode.Length * 13, 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random rm_noise = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪声线
for (int i = 0; i < 25; i++)
{
int x1 = rm_noise.Next(image.Width);
int x2 = rm_noise.Next(image.Width);
int y1 = rm_noise.Next(image.Height);
int y2 = rm_noise.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
for (int i = 0; i < 100; i++)//画图片的噪声点
{
int x = rm_noise.Next(image.Width);
int y = rm_noise.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(rm_noise.Next()));
}
//在图片上写验证码
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkBlue, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
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, ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
2.调用的页面
yzm.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yzm.aspx.cs" Inherits="yzm" %>
<!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>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="image.aspx" /><br />
<asp:Button ID="Btn_check" runat="server" Text="验证" OnClick="Btn_check_Click" /></div>
</form>
</body>
</html>
yzm.aspx.cs<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yzm.aspx.cs" Inherits="yzm" %>
<!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>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="image.aspx" /><br />
<asp:Button ID="Btn_check" runat="server" Text="验证" OnClick="Btn_check_Click" /></div>
</form>
</body>
</html>
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class yzm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_check_Click(object sender, EventArgs e)
{
Response.Write(Session["yzmCode"].ToString());
if (TextBox1.Text.CompareTo(Request.Cookies["yzmCode"].Value.ToString())==1)
{
Response.Write("<script>alert('验证码错误!')</script>");
}
else
{
Response.Write("<script>alert('验证码正确!')</script>");
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class yzm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_check_Click(object sender, EventArgs e)
{
Response.Write(Session["yzmCode"].ToString());
if (TextBox1.Text.CompareTo(Request.Cookies["yzmCode"].Value.ToString())==1)
{
Response.Write("<script>alert('验证码错误!')</script>");
}
else
{
Response.Write("<script>alert('验证码正确!')</script>");
}
}
}