Checkcode.aspx create a new file, the page do not write anything in the code, Page_Load written the following code:
String = chkCode string.Empty;
int ix, iy;
ix = 80;
iy = 24-;
// color list, a verification code, line noise, noise
Color [] = {Color.Black Color, Color.Red, Color.Blue, Color.green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue};
// list of fonts, codes for
String [] = {font "Times New Roman", "the MS Mincho", "Book Antiqua", "Gungsuh", "the MingLiU", "Arial"};
// verification code characters, removing some confusing character
char [] character = { '2 ', '3', '4', '5', '6', '8', '9', 'a', 'B', ' C ',' D ',' E ',' F ', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(ix, iy);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(ix);
int y1 = rnd.Next(iy);
int x2 = rnd.Next(ix);
int y2 = rnd.Next(iy);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 14, FontStyle.Bold);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 16 + 2, (float)2);
}
//画噪点
for (int i = 0; i < 50; 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);
}
// write the codes the SESSION
the Session [ "checkcode"] = chkCode.ToLower ();
// clear the page output cache, the cache no page provided
the Response.Buffer = to true;
Response.ExpiresAbsolute = System.DateTime.Now .AddMilliseconds (0);
Response.Expires = 0;
Response.CacheControl = "NO-Cache";
Response.AppendHeader ( "Pragma", "No-the Cache");
// memory write verification picture stream, and its output "image / Png" format
String = chkCode string.Empty;
int ix, iy;
ix = 80;
iy = 24-;
// color list, a verification code, line noise, noise
Color [] = {Color.Black Color, Color.Red, Color.Blue, Color.green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue};
// list of fonts, codes for
String [] = {font "Times New Roman", "the MS Mincho", "Book Antiqua", "Gungsuh", "the MingLiU", "Arial"};
// verification code characters, removing some confusing character
char [] character = { '2 ', '3', '4', '5', '6', '8', '9', 'a', 'B', ' C ',' D ',' E ',' F ', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(ix, iy);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(ix);
int y1 = rnd.Next(iy);
int x2 = rnd.Next(ix);
int y2 = rnd.Next(iy);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 14, FontStyle.Bold);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 16 + 2, (float)2);
}
//画噪点
for (int i = 0; i < 50; 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);
}
// write the codes the SESSION
the Session [ "checkcode"] = chkCode.ToLower ();
// clear the page output cache, the cache no page provided
the Response.Buffer = to true;
Response.ExpiresAbsolute = System.DateTime.Now .AddMilliseconds (0);
Response.Expires = 0;
Response.CacheControl = "NO-Cache";
Response.AppendHeader ( "Pragma", "No-the Cache");
// memory write verification picture stream, and its output "image / Png" format
MS = new new the MemoryStream the MemoryStream ();
the try
{
bmp.Save (MS, ImageFormat.Png);
Response.ClearContent ();
Response.ContentType = "Image / Png File";
the Response.BinaryWrite (ms.ToArray ());
}
the finally
{
// explicitly release resources
bmp.Dispose ();
g.Dispose ();
}
in login.aspx added Image control provided ImageUrl = "checkcode.aspx" can.
<asp: Image ID = "Image1 " runat = "server" οnclick = ImageAlign = "Middle" ToolTip = " see me, tap me!" / "this.src = this.src + '?'">
Note that one of the onclick attribute TooTip and very useful! Or use:
<span style = "cursor: pointer ; text-decoration: underline;" οnclick = "document.getElementById ( 'Image1') src = document.getElementById ( 'Image1') src +.. '?'"> a transducer </ span>
input detection, can read the text input box, and Session value is compared.
the try
{
bmp.Save (MS, ImageFormat.Png);
Response.ClearContent ();
Response.ContentType = "Image / Png File";
the Response.BinaryWrite (ms.ToArray ());
}
the finally
{
// explicitly release resources
bmp.Dispose ();
g.Dispose ();
}
in login.aspx added Image control provided ImageUrl = "checkcode.aspx" can.
<asp: Image ID = "Image1 " runat = "server" οnclick = ImageAlign = "Middle" ToolTip = " see me, tap me!" / "this.src = this.src + '?'">
Note that one of the onclick attribute TooTip and very useful! Or use:
<span style = "cursor: pointer ; text-decoration: underline;" οnclick = "document.getElementById ( 'Image1') src = document.getElementById ( 'Image1') src +.. '?'"> a transducer </ span>
input detection, can read the text input box, and Session value is compared.