webform 一般处理程序,图片验证码

一般处理程序ashx.cs里的代码

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Drawing;

using System.Web.SessionState;  //IRequiersSessinState的命名空间



namespace Web自己写验证码

{

    /// <summary>

    /// Handler1 的摘要说明

    /// </summary>

    public class Handler1 : IHttpHandler,IRequiresSessionState

    {



        public void ProcessRequest(HttpContext context)

        {

            //要输出的格式

           context.Response.ContentType = "img/jpeg";

            //context.Response.Write("Hello World");



           Bitmap img = new Bitmap(60,30);

           Graphics gr = Graphics.FromImage(img);

           Font font = new Font("宋体",12,FontStyle.Bold);

           SolidBrush brush = new SolidBrush(Color.White);

           brush.Color = Color.Red;



           string s = " 123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";

           string str = " ";

           Random rand = new Random();

           for (int i = 0; i < 4; i++)

           {

               int start = rand.Next(62);

               str += s.Substring(start, 1).ToString();

           }



            //将随机生成的验证码放在Session里

           context.Session["yanzheng"] = str;







           gr.DrawString(str,font,brush,0,0);

           img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);





        }



        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}


aspx.cs里的代码

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace Web自己写验证码

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {



        }



        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

        {

            //用超链接传值来改变验证码

            ImageButton1.ImageUrl = "Handler1.ashx?id=" + new Random().Next(100);

        }



        protected void Button1_Click(object sender, EventArgs e)

        {



            //输入的验证码和Session里的验证码比较

            if (TextBox1.Text.ToUpper().Trim() == Session["yanzheng"].ToString().ToUpper().Trim())

            {

                Response.Write("<script>alert('登录成功')</script>");

            }

            else

                Response.Write("<script>alert('登录失败')</script>");

        }

    }

}


界面里的代码

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Web自己写验证码.WebForm1" %>



<!DOCTYPE html>



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    

    &nbsp;

        <br />

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <br />

        <br />

        <br />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>

                &nbsp; 验证码:&nbsp;

                <asp:TextBox ID="TextBox1" runat="server" Height="28px"></asp:TextBox>

                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;





                重点!!!!!!

                <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Handler1.ashx" OnClick="ImageButton1_Click" />

            </ContentTemplate>

        </asp:UpdatePanel>

        <br />

        <br />

        <br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <br />

        <br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="注册" />

    

    </div>

    </form>

</body>

</html>

 

你可能感兴趣的:(webform)