CSDN博客

img bccu

“验证码”的实现

发表于2004/9/17 10:45:00  672人阅读

--showcode.aspx

<asp:Image id="Image1" runat="server" ImageUrl="Code.aspx"></asp:Image>

---code.aspx

<%@ import namespace="System"%>
<%@ import namespace="System.IO"%>
<%@ import namespace="System.Drawing"%>
<%@ import namespace="System.Drawing.Imaging"%>
<%@ import namespace="System.Drawing.Drawing2D"%>
<script language="C#" runat="server">
private Bitmap validateimage;
private Graphics g;
 public void  Page_Load(object Sender   ,EventArgs e  )
 {
    if (!IsPostBack){
    string  VNum  =MakeValidateCode( );
 Session["VNum"]=VNum; 
    ValidateCode(VNum);
 }
}

public void  ValidateCode(string VNum)
{
 validateimage = new Bitmap(70, 20, PixelFormat.Format24bppRgb);
 g = Graphics.FromImage(validateimage);

 g.DrawString(VNum, new Font("宋体",15),new SolidBrush(Color.White),new PointF(8,0));
 g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(110,20), Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,60,40)),0,0,120,30);
 g.Save();
 MemoryStream ms=new MemoryStream();
 validateimage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 Response.ClearContent();
 Response.ContentType="image/gif";
 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','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 num = "";
 Random r = new Random();
 for(int i = 0; i < 5; i++)
 {
  num += s[r.Next(0, s.Length)].ToString();
 }
 return num;
}
</script>

0 0

相关博文

我的热门文章

img
取 消
img