日期:2014-05-20  浏览次数:20464 次

谁给个验证码的完整代码谢谢!!!
谁给个验证码的完整代码十分感谢!!!

------解决方案--------------------
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string VNum = " ";
VNum = this.RndNum(4);
Session[ "VNum "] = VNum;
this.ValidataCode(VNum);
}

public void ValidataCode(string str)
{
System.Drawing.Bitmap Img;
Graphics g;
MemoryStream ms;
int gheight = str.Length*11;
Img = new Bitmap(gheight,20);
g = Graphics.FromImage(Img);
SolidBrush sb = new SolidBrush(Color.DarkSlateBlue);
Font ft = new Font( "Arial ",10);
g.DrawString(str,ft,sb,3,3);
ms = new MemoryStream();
Img.Save(ms,ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png ";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}

public string RndNum(int i)
{
string VNum = " ";
Random Rnd = new Random();
string Vchar = "0,1,2,3,4,5,6,7,8,9 ";
string[] Vc = Vchar.Split( ', ');
for(int k =0;k <i;k++)
{
VNum = VNum + Vc[Rnd.Next(0,9)];
}
return VNum;
}
------解决方案--------------------
-------------------Validate.aspx------------------

<%@ Page Language= "C# " %>
<%@ import namespace= "System "%>
<%@ import namespace= "System.IO "%>
<%@ import namespace= "System.Drawing "%>
<%@ import namespace= "System.Drawing.Imaging "%>
<%@ import namespace= "System.Drawing.Drawing2D "%>

<script runat= "server ">
private Bitmap validateimage;
private Graphics g;
public void Page_Load(object Sender ,EventArgs e ){
Response.BufferOutput = true; //特别注意
Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
Response.AppendHeader( "Pragma ", "No-Cache "); //特别注意
string VNum =MakeValidateCode( );
Session[ "VNum "]=VNum;//取得验证码,以便后来验证
ValidateCode(VNum);}
public void ValidateCode(string VNum)
{
validateimage = new Bitmap(60, 20, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(110,20), Color.FromArgb(240,255,255,255),Color.FromArgb(240,255,255,255)),0,0,200,200);
g.DrawString(VNum, new Font( "arial ",11),new SolidBrush(Color.Red),new PointF(6,0));
g.Save();
MemoryStream ms=new MemoryStream();
validateimage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType= "image/bmp ";
Response.BinaryWrite(ms.ToArray());
Response.End();
}

string MakeValidateCode()
{
char[] s = new char[]{ '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 '};