验证码 点击刷新

<img src="CreateCheckCode.aspx?" alt="" title="点击刷新" onclick="this.src+=Math.random()" />    //CreateCheckCode.aspx为一个单独的页面 来产生验证码的
protected void Page_Load(object sender, EventArgs e)
    {

        Random ran = new Random();
        Bitmap bmp = new Bitmap(45, 18);
        Graphics g = Graphics.FromImage(bmp);
        g.Clear(Color.White);
        Single iSize = 12.0F;
        Font ifont = new Font(new FontFamily("宋体"), iSize);
        string CheckCode = "";
        string drawCode = "";
        int iW = 9;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        for (int i = 0; i < 4; i++)
        {
            drawCode = Code(ran, 9).ToString();
            CheckCode += drawCode;
            g.DrawString(drawCode, ifont, new SolidBrush(Color.Black), i * iW + 1, 1);
        }
        if (Session["CheckCode"] == null)
        {
            Session.Add("CheckCode", CheckCode);      //将验证码存入session中
        }
        else
        {
            Session["CheckCode"] = CheckCode;        
        }
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
     Response.End(); //将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 EndRequest 事件。 }
private int Code(Random ran, int value) { return ran.Next(9); }

本文来自博客园,作者:mushishi,转载请注明原文链接:https://www.cnblogs.com/mushishi/archive/2013/05/17/3083714.html

原文地址:https://www.cnblogs.com/mushishi/p/3083714.html