identificandocode.aspx 里没有内容略。
identificandocode.cs文件:
usando o sistema;
usando System.Data;
usando System.Configuration;
usando System.Collections;
usando System.Web;
usando System.Web.Security;
usando System.Web.UI;
usando System.Web.UI.WebControls;
usando System.Web.UI.WebControls.WebParts;
usando System.Web.UI.HtmlControls;
usando System.Drawing;
usando System.Drawing.Imaging;
usando System.IO;
código de identificação de classe parcial pública: System.Web.UI.Page
{
protegido void Page_Load (remetente do objeto, EventArgs e)
{
string tmp = RndNum(4);
HttpCookie a = new HttpCookie("ImageV", tmp);
Response.Cookies.Add(a);
this.ValidateCode(tmp);
}
validateCode vazio privado (string VNum)
{
Bitmap Img = nulo;
Gráficos g = nulo;
MemoryStream ms = nulo;
int galtura = VNum.Comprimento * 12;
Img = novo Bitmap(altura, 25);
g = Gráficos.FromImage(Img);
//背景颜色
g.Clear(Color.LightSteelBlue);
//文字字体
Fonte f = nova Fonte("Arial Black", 10);
//文字颜色
SolidBrush s = novo SolidBrush(Color.RoyalBlue);
g.DrawString(VNum, f, s, 3, 3);
ms = novo MemoryStream();
Img.Save(ms, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "imagens/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
string privada RndNum(int VcodeNum)
{
string Vchar = "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,v,w,x,y,z";
string[] VcArray = Vchar.Split(new Char[] { ',' });
string VNum = "";
temperatura interna = -1;
Rand aleatório = new Random();
for (int i = 1; i <VcodeNum + 1; i++)
{
se (temperatura! = -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temperatura! = -1 && temp == t)
{
retornar RndNum(VcodeNum);
}
temperatura = t;
VNum += VcArray[t];
}
retornar VNum;
}
}
使用方法:
在需要它的页面html里添加
<img src="identifyingcode.aspx" />
Cookies HttpCookieCollection = Request.Cookies;
string tmp = cookies["ImageV"].Valor;
然后比tmp与获取的较验证码文本框中的值是否相同
Link: http://enuosky.cnblogs.com/archive/2006/05/15/400954.html