using System;
using Syste
m.Collection
s.Generi
c; using Syste
m.Linq;
using Syste
m.Web;
using Syste
m.Web.UI;
using Syste
m.Web.UI.WebControls;
using Syste
m.Drawing;
namespace 学生在线
考试系统
{ public partial class AjaxAutoCode : Syste
m.Web.UI.Page
{ //验证数字
public
String authcode =
String.Empty;
protected void Page_Load(ob
ject sender,EventArgs
E) { #region 第一种产生验证码的方法
Random random = new Random(
); authcode = rando
m.Next(1111,999
9).To
String(
); //构造图片
Bitmap image = new Bitmap(authcode.Length * 12,25
); //创建画布
Graphics g = Graphic
s.FromImage(imag
E);
try
{ g.Clear(Color.Whit
E);
for (int i = 0; i < 2
5; i++)
{ int x1 = rando
m.Next(image.Width
); int x2 = rando
m.Next(image.Width
); int y1 = rando
m.Next(image.Height
); int y2 = rando
m.Next(image.Height
); //链接两点的线条
g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2
); }
Font font = new Font("Arial",12,FontStyle
.bold | FontStyle.Italic
); Syste
m.Drawing.Drawing2D.LinearGradientBrush brush = new Syste
m.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0,image.Width,image.Height),Color
.blue,Color.DarkBlue,1.2f,tru
E);
g.Draw
String(authcode,font,brush,2,2
); //画图片的前景噪点
for (int i = 0; i < 100; i++)
{ int x = rando
m.Next(image.Width
); int y = rando
m.Next(image.Height
); image.SetPixel(x,y,Color.FromArgb(rando
m.Next())
); }
g.DrawRectangle(new Pen(Color.Silver),image.Width - 1,image.Height - 1
); Syste
m.IO.MemoryStream ms = new Syste
m.IO.MemoryStream(
); image.Save(ms,Syste
m.Drawing.Imaging.ImageFormat.Gif
); @H_189_0
@ms.WriteTo(thi
s.Response
.outputStream
); @H_189_0
@ms.Close(
); thi
s.Response.ContentType = "image/gif";
}
finally
{ image.Dispose(
); g.Dispose(
); }
#endregion
}
}
}
function fGetCode()
{ document.getElementById("getcode").src="Default2.aspx?"+Math.random(
); }
<label>验证码</label>
<asp:TextBox ID="txt_
checkCode" runat="server" Width="178px"></asp:TextBox>
<img src="Default2.aspx" alt="看不清楚?" id="getcode"/> <a href="javascript:fGetCode()">更换验证码</a>