zoukankan      html  css  js  c++  java
  • WebForm带有图片的验证码

    验证码形成的部分在一个aspx文件中:

    页面设置

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!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>
        <style>
            #Image1 {
            cursor:pointer;
            }
    
    
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        验证码:<asp:Image ID="Image1" runat="server" ImageUrl="tupian.aspx" /><%--ImageUrl指向一个Web窗口tupian.aspx 在该窗体中处理验证码--%>
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /><br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><%--输入验证码文本区--%>
            <asp:Button ID="Button1" runat="server" Text="登录" />
        </div>
        </form>
    </body>
    </html>
    <script>
        var a = 0;//a的作用是使每次点击验证码图片都会变
        document.getElementById("Image1").onclick = function () {//js更改属性,如果更改后的属性和以前的属性一样,不变
            this.setAttribute("src", "tupian.aspx?id=" + a);
            a++;
        };
    
    
    
    </script>
    View Code

    页面功能代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Trim().ToUpper() ==  Session["yzm"].ToString().ToUpper())
            {
                Label1.Text = "正确";
            }
            else
            {
                Label1.Text = "错误";
            }
        }
    }
    View Code

    tupian.aspx代码页面(重点

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    
    public partial class tupian : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //创建图片。位图
            Bitmap bmap = new Bitmap(120,50);//宽:100px ;高:50px
            
            //绘制画布
            Graphics g = Graphics.FromImage(bmap);
           
            Random r = new Random();
            //创建颜色集合
            List<Color> clist = new List<Color>();
            clist.Add(Color.Peru);
            clist.Add(Color.Purple);
            clist.Add(Color.Pink);
            clist.Add(Color.PaleVioletRed);
    
            //绘制水印照片背景
            g.FillRectangle(new SolidBrush(clist[r.Next(0,clist.Count)]),0,0,120,50);
            for (int h = 0; h < 6; h++)//位置不同,可以设置干扰线覆盖文字,还是文字覆盖干扰线
            {
                //绘制干扰线
                Pen p = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]), r.Next(0, 8));
                g.DrawLine(p, r.Next(0, 120), r.Next(0, 50), r.Next(0, 120), r.Next(0, 50));
            }
            //绘制验证码文字
            string s = "";
            string ss = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            for (int i = 0; i < 4; i++)
            {
                s += ss.Substring(r.Next(0,ss.Length),1);
            }
            Session["yzm"] = s;//传验证码的值
            //绘制文字格式和大小
            Font f = new Font("宋体",30);
            //绘制画笔颜色,即文字颜色
            Brush b = new SolidBrush(System.Drawing.Color.Red);
            //绘制水印
            g.DrawString(s,f,b,1,5);
            //将水印照片存在”流“中,并且设定照片格式后缀
            bmap.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
            //停止输出
            Response.End();
        }
    }
    View Code

    完!!

  • 相关阅读:
    堆排序(Heap Sort)
    快速排序(Quick Sort)
    希尔排序(Shell Sort)
    C和C++中的可变参数及宏的使用
    函数中的参数问题小结(&,*,传参与变参)
    C语言基础之struct
    C语言基础之指针
    从名字开始讲——C与C++的编译细节
    二维数组的动态初始化与复制
    《Java程序设计》第二次学习总结
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/6077374.html
Copyright © 2011-2022 走看看