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

 

完!!

 

转载于:https://www.cnblogs.com/wwz-wwz/p/6077374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值