Create a new page specifically used to create verification code pictures
Its background cs file code is as follows:
PageLoad
private void Page_Load(object sender, e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
Where CreateRandomCode is a custom function, and the parameters represent the number of verification code bits
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = (',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)));
}
int t = (35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage is also a custom function used to generate graphs.
private void CreateImage(string checkCode)
{
int iwidth = (int)( * 11.5);
image = new (iwidth, 20);
Graphics g = (image);
Font f = new ("Arial", 10, );
Brush b = new ();
//(new (),0,0,, );
();
(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(, 0);
Random rand = new Random();
for (int i=0;i<5;i++)
{
int y = ();
(blackPen,0,y,,y);
}
ms = new ();
(ms,);
();
= "image/Jpeg";
(());
();
();
}
//(new (),0,0,, );
();
Both methods can change the background color of the generated image. The following for loop is used to generate some random horizontal lines
Just add a <asp:Image> control to the page where the verification code is needed, but point ImageUrl to the page where the verification code is generated.
<asp:Image Runat="server" ID="ImageCheck" ImageUrl=""></asp:Image>
Its background cs file code is as follows:
PageLoad
Copy the codeThe code is as follows:
private void Page_Load(object sender, e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
Where CreateRandomCode is a custom function, and the parameters represent the number of verification code bits
Copy the codeThe code is as follows:
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = (',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)));
}
int t = (35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage is also a custom function used to generate graphs.
Copy the codeThe code is as follows:
private void CreateImage(string checkCode)
{
int iwidth = (int)( * 11.5);
image = new (iwidth, 20);
Graphics g = (image);
Font f = new ("Arial", 10, );
Brush b = new ();
//(new (),0,0,, );
();
(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(, 0);
Random rand = new Random();
for (int i=0;i<5;i++)
{
int y = ();
(blackPen,0,y,,y);
}
ms = new ();
(ms,);
();
= "image/Jpeg";
(());
();
();
}
//(new (),0,0,, );
();
Just add a <asp:Image> control to the page where the verification code is needed, but point ImageUrl to the page where the verification code is generated.
Copy the codeThe code is as follows:
<asp:Image Runat="server" ID="ImageCheck" ImageUrl=""></asp:Image>