First, create a new validate ASPX validation page. Then generate the Captcha control ImageButton, and then generate the picture form.
The code is as follows:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Drawing;
Using System.IO;
public partial class Picture:System.Web.UI.Page
{
Random ran = new random ();
protected void Page_Load (object sender, EventArgs e)
{
String str = getrandomvalidate (4);
session["Checkcode"] = str;//This part is Wie the verification code is written to the session, for verification, you can also use cookies
Getimagevalidate (str);
}
Get random string, length custom
private string getrandomvalidate (int len)
{
int num;
int tem;
String Rtustr= "";
for (int i = 0; i < len;i++)
{
num = ran. Next ();
TEM = num% 10 + ' 0 ';//Generate numbers
TEM = num% + ' A ';//Generate characters
Rtustr + = Convert.tochar (TEM). ToString ();public partial class Picture:System.Web.UI.Page
{
Random ran = new random ();
protected void Page_Load (object sender, EventArgs e)
{
String str = getrandomvalidate (4);
session["Checkcode"] = str;//This part is Wie the verification code is written to the session, for verification, you can also use cookies
Getimagevalidate (str);
}
Get random string, length custom
private string getrandomvalidate (int len)
{
int num;
int tem;
String Rtustr= "";
for (int i = 0; i < len;i++)
{
num = ran. Next ();
TEM = num% 10 + ' 0 ';//Generate numbers
TEM = num% + ' A ';//Generate characters
Rtustr + = Convert.tochar (TEM). ToString ();public partial class Picture:System.Web.UI.Page
{
Random ran = new random ();
protected void Page_Load (object sender, EventArgs e)
{
String str = getrandomvalidate (4);
session["Checkcode"] = str;//This part is Wie the verification code is written to the session, for verification, you can also use cookies
Getimagevalidate (str);
}
Get random string, length custom
private string getrandomvalidate (int len)
{
int num;
int tem;
String Rtustr= "";
for (int i = 0; i < len;i++)
{
num = ran. Next ();
TEM = num% 10 + ' 0 ';//Generate numbers
TEM = num% + ' A ';//Generate characters
Rtustr + = Convert.tochar (TEM). ToString ();
}
return rtustr;
}
Generate images
private void Getimagevalidate (string strvalue)
{
String str=oo00; The first two is the letter O, the last two digits are 0
int width = Convert.ToInt32 (strvalue.length*12);
Bitmap img = new Bitmap (width,23);
Graphics GFC = Graphics.fromimage (IMG);
Gfc. Clear (Color.White);
DrawLine (GFC,IMG);
Write the verification code to define the font
Font font = new Font ("Arial", 12,fontstyle.bold);
Font font = new Font ("Arial", 12,fontstyle.bold| Fontstyle.italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rectangle (0,0,img. Width,img. Height), color.darkorchid,color.blue,1.5f,true);
Gfc. DrawString (Strvalue,font, brush, 3,2);
Drawpoint (IMG);
Gfc. DrawRectangle (New Pen (Color.darkblue), 0,0,img. Width-1,img. HEIGHT-1);
Add an image to a page
MemoryStream ms = new MemoryStream ();
Img. Save (ms,system.drawing.imaging.imageformat.gif);
Change the HTTP
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
Dispose
Gfc. Dispose ();
Img. Dispose ();
Response.End ();
}
private void DrawLine (Graphics gfc,bitmap img)
{
Choose to draw 10 lines, you can also add, or do not line, as long as the random noise point on the line
for (int i = 0; i < 10;i++)
{
int x1 = ran. Next (IMG. Width);
int y1 = ran. Next (IMG. Height);
int x2 = ran. Next (IMG. Width);
int y2 = ran. Next (IMG. Height);
Gfc. DrawLine (New Pen (Color.silver), x1,y1,x2,y2);//Note The brush should be light, otherwise you can not see clearly
}
}
private void Drawpoint (Bitmap img)
//{
//}
private void Drawpoint (Bitmap img)
{
int col = ran. Next ();//the same color in a picture
for (int i = 0; i <; i++)
{
int x = ran. Next (IMG. Width);
int y = ran. Next (IMG. Height);
Img. SetPixel (X,y,color.fromargb (col));
}
}
}
This is all using the validation window source code,
How to implement the login interface to generate the verification code for ASP