登录验证码,比较简单

来源:互联网 发布:留学文书怎么写 知乎 编辑:程序博客网 时间:2024/06/06 06:28
html:
 <table>        <tr>            <td align="left">                验证码:            </td>            <td align="left">                <asp:TextBox ID="txtCheckNum" runat="server"></asp:TextBox>                <img src="Handler.ashx" name="Pic" style="cursor: pointer" onclick="this.src='Handler.ashx'"                    alt="点击刷新" />                <asp:Button ID="btnSure" runat="server" Text="提交" OnClick="btnSure_Click" />            </td>        </tr>    </table>


ashx:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Drawing;using System.Drawing.Imaging;using System.Web.SessionState;namespace WebApplication3{    /// <summary>    /// $codebehindclassname$ 的摘要说明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    public class Handler : IHttpHandler, IRequiresSessionState     {        public void ProcessRequest(HttpContext context)        {            //context.Response.ContentType = "text/plain";            //context.Response.Write("Hello World");            Bitmap NewBmp = new Bitmap(36, 16, PixelFormat.Format32bppArgb);            Graphics g = Graphics.FromImage(NewBmp);            g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, 36, 20));            Font textFont = new Font("GB2312", 10);            Rectangle rectangle = new Rectangle(0, 0, 36, 20);            string ThisNum = Convert.ToString(GetNum());            context.Session["CheckNum"] = ThisNum;            g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);            g.DrawString(ThisNum, textFont, new SolidBrush(Color.Blue), rectangle);            NewBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);         }        public bool IsReusable        {            get            {                return false;            }        }        public int GetNum()        {            int GiveNum;            Random MyNum = new Random();            GiveNum = MyNum.Next(8999) + 1000;            return GiveNum;        }    }}


0 0