怎样用C#编写验证码的使用程序。
{
privateString cap_save;
privateGraphics g;
PictureBox pic;
Bitmap bt;
publicCAPTCHA(PictureBox picControl)
{
pic = picControl;
bt = newBitmap(pic.Width,pic.Height);
g= Graphics.FromImage(bt);
g.Clear(Color.White);
this.show();
}
publicvoidshow()
{
String temp = "123456789abcdefghijkLmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
Random rd = newRandom();
cap_save = "";
g.Clear(Color.FromArgb(240, 240, 240));
for(inti = 0; i < 4; i++)
{cap_save += temp.Substring(rd.Next(0, 57), 1);
Font font = newFont("黑体",rd.Next(18, 20), FontStyle.Italic | FontStyle.Bold);
Color color = Color.FromArgb(rd.Next(0,200), rd.Next(0, 200), rd.Next(0, 200));
SolidBrushbrush = newSolidBrush(color);
PointF point = newPointF(i * rd.Next(12, 15) + 10, rd.Next(1, pic.Height - 25));
g.DrawString(cap_save.Substring(i, 1), font, brush, point);
Pen pen = newPen(brush, 1);
color = Color.FromArgb(rd.Next(100, 200),rd.Next(100, 200), rd.Next(100, 200));
brush = newSolidBrush(color);
g.DrawLine(pen, rd.Next(-5, pic.Width + 5), rd.Next(-5, pic.Height + 5),rd.Next(-5, pic.Width + 5), rd.Next(-5, pic.Height + 5));
for (intj = 0; j < 6; j++)
{
color = Color.FromArgb(rd.Next(0, 200), rd.Next(0,200), rd.Next(0, 200));
brush = newSolidBrush(color);
pen = newPen(brush,1);
int x = rd.Next(0, pic.Width);
int y = rd.Next(0, pic.Height);
g.DrawLine(pen, x, y, x + 1, y);
}
pic.Image = bt;
}
}
publicvoidshow2()
{
pic.Image = bt;
}
publicBoolean check(String str)
{
if(str.ToLower() == cap_save.ToLower())
{
returntrue;
}
else
{
this.show();
returnfalse;
}
}
求大神帮助。我是编程小白 展开
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.Imaging;
using System.IO;
public partial class Sate_Manager_GenerateCheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(58, 25);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
/**/
////画噪线
//for (int i = 0; i < 10; i++)
//{
// int x1 = rnd.Next(100);
// int y1 = rnd.Next(40);
// int x2 = rnd.Next(100);
// int y2 = rnd.Next(40);
// Color clr = color[rnd.Next(color.Length)];
// g.DrawLine(new Pen(clr), x1, y1, x2, y2);
//}
//画验证码字符串
Session["code"] = chkCode;
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 15);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 12 + 4, (float)5);
}
//画噪点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//清除该页输出缓存,设置该页无缓存
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
//将验证码图片写入内存流,并将其以 "image/Png" 格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}
}
}
}
前台调用:
function change(obj)
{
obj.src=obj.src+'?';
}
<input type="text" style="width: 57px" id="txtverifycode" runat="server"> <img style=" cursor:pointer" src="GenerateCheckCode.aspx" onclick="change(this)" title="看不清楚?换张图片请点击!" />