zoukankan      html  css  js  c++  java
  • 第十节 19验证码案例 简单

    <%@ Page Language="C#" AutoEventWireup="true"
        CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
    <form runat="server">  
        <image src="Image.ashx" onClick="this.src='Image.ashx?aaa=aaa'"></image>
        验证码:<asp:TextBox ID="TextBox1" runat="server" onClick="this.src='Image.ashx?aa='+new Date()"></asp:TextBox>
        
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">登 陆</asp:LinkButton>
    
        </form>
    </body>
    </html>
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string old_code = Convert.ToString(Session["code"]);
            if (old_code == TextBox1.Text)
            {
                Response.Write("验证成功!");
            }
            else {
                Response.Write("验证失败1");
                
            }
        }
    }
    

      

    <%@ WebHandler Language="C#" Class="Image" %>
    
    using System;
    using System.Web;
    
    //在一般应用程序中使用session要引用System.Web.SessionState.IRequiresSessionState
    
    public class Image : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        
        public void ProcessRequest (HttpContext context) {
            
            context.Response.ContentType = "image/JPEG";
            //创建一个画布
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 30)) 
            {
                //创建一个图片格式
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 
                {
                    /*g.DrawString("平安北京", new System.Drawing.Font("宋体", 16), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0));
                    g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(10, 10, 10, 10));
    
                    System.Drawing.Pen pen = (System.Drawing.Pen)System.Drawing.Pens.Red.Clone();
                    pen.Width = 3;
    
                    g.DrawEllipse(pen, new System.Drawing.Rectangle(20, 20, 10, 10));
                    bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);*/
    
                    Random rand = new Random();
                    int code = rand.Next(10000, 99999);
                    string strCode = code.ToString();
    
                    HttpContext.Current.Session["code"] = strCode;
    
                    g.DrawString(strCode, new System.Drawing.Font("宋体", 16), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0));
                    g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(10, 10, 10, 10));
                    bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    
    
    
                }
            
            }
            //context.Response.Write("Hello World");
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }
    

      

  • 相关阅读:
    HDU 4436 str2int (后缀自动机SAM,多串建立)
    HDU 4498 Function Curve (自适应simpson)
    PHP实现微信商户支付企业付款到零钱功能代码实例
    微信支付现金红包接口应用实例代码说明和DEMO详解,适合用来做微信红包营销活动、吸粉利器
    java开发学生信息管理系统的实现(简洁易懂),适合计算机专业学生参考,课程设计、毕业论文设计参考等
    公司注册经营范围大全
    《胡雪岩》影评10篇
    提高淘宝店铺动态评分的四大技巧
    为什么我的淘宝店铺动态评分清零了?
    CC攻击原理及防范方法和如何防范CC攻击
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2405352.html
Copyright © 2011-2022 走看看