作者在 2010-08-25 23:06:13 发布以下内容
只需要在你要显示验证码的页面上拖一个imagebutton
<asp:Image ID="Image1" runat="server" ImageUrl="xxx.aspx" />
下面代码就是xxx.aspx的xxxx.cs代码:
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.IO;
using System.Drawing.Imaging;
public partial class yanzhengma : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
draw(getwenzi());
}
}
public void draw(string wenzi)
{
Bitmap b = new Bitmap((int)Math.Ceiling(wenzi.Length * 15.5), 25);//定义一个图像
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Microsoft Sans Serif", "Arial","Comic Sans MS", "Verdana", };
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.Next(b.Width);
int y = rand.Next(b.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
Brush bu = new SolidBrush(Color.Black);
int yanse = rand.Next(6);
int ziti = rand.Next(4);
Brush ss = new SolidBrush(c[yanse]);//画刷
Font f = new Font(font[ziti],14,FontStyle.Italic);//文字的格式
g.DrawString(wenzi, f, ss, 3, 3);
MemoryStream ms = new MemoryStream();//定义一个内存流
b.Save(ms, ImageFormat.Png);
Response.ClearContent();//
Response.ContentType = "image/png";
Response.BinaryWrite(ms.ToArray());
}
public string getwenzi()//获取字符
{
string[] zifu = new string[] { "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", };
string strcode = null;
Random ron = new Random();//定义一个随机
for (int i = 0; i < 4; i++)
{
int a = ron.Next(0, 35);//产生字符随机数的范围
strcode += zifu[a];
}
Session["img"] = strcode;
return strcode;
}
}
<asp:Image ID="Image1" runat="server" ImageUrl="xxx.aspx" />
下面代码就是xxx.aspx的xxxx.cs代码:
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.IO;
using System.Drawing.Imaging;
public partial class yanzhengma : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
draw(getwenzi());
}
}
public void draw(string wenzi)
{
Bitmap b = new Bitmap((int)Math.Ceiling(wenzi.Length * 15.5), 25);//定义一个图像
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Microsoft Sans Serif", "Arial","Comic Sans MS", "Verdana", };
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.Next(b.Width);
int y = rand.Next(b.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
Brush bu = new SolidBrush(Color.Black);
int yanse = rand.Next(6);
int ziti = rand.Next(4);
Brush ss = new SolidBrush(c[yanse]);//画刷
Font f = new Font(font[ziti],14,FontStyle.Italic);//文字的格式
g.DrawString(wenzi, f, ss, 3, 3);
MemoryStream ms = new MemoryStream();//定义一个内存流
b.Save(ms, ImageFormat.Png);
Response.ClearContent();//
Response.ContentType = "image/png";
Response.BinaryWrite(ms.ToArray());
}
public string getwenzi()//获取字符
{
string[] zifu = new string[] { "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", };
string strcode = null;
Random ron = new Random();//定义一个随机
for (int i = 0; i < 4; i++)
{
int a = ron.Next(0, 35);//产生字符随机数的范围
strcode += zifu[a];
}
Session["img"] = strcode;
return strcode;
}
}