网页设计——验证码

CreateCheckCode.aspx

 <asp:Label ID="Label4" runat="server" Text="验证码"></asp:Label>
                             <asp:TextBox ID="tbx_yzm" runat="server" Width="86px"></asp:TextBox>
                             <asp:ImageButton ID="ibtn_yzm" src="/ImageCode.aspx"  runat="server" Width="70px" Height="40px" OnClick="ibtn_yzm_Click" />        
                    <script type="text/javascript">
                    function changeCode() 
                    {
                    document.getElementById('ibtn_yzm').src = document.getElementById('ibtn_yzm').src + '?';
                    }
                    </script>
                             <asp:Button ID="Button3" runat="server" Text="刷新" OnClick="Button3_Click"  />

CreateCheckCode.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public partial class CreateCheckCode : System.Web.UI.Page
{
    Random ran = new Random();
    private string CreateCheckCodeString()
    { //定义用于验证码的字符数组
        char[] AllCheckCodeArray = { '1', '2', '3','4', '5', '6', '7', '8', '9', '0',
            'A', 'B', 'C','D', 'E', 'F', 'G', 'H', 'I', 'J', 'K','L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f'
        , 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'
        , 'y', 'z'};
        //定义验证码字符串
        string randomcode = "";
        Random rd = new Random();
        //生成4位验证码字符串
        for (int i = 0; i < 4; i++)
            randomcode += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)];
        return randomcode;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string str = CreateCheckCodeString();
        Session["CheckCode"] = str;
        int ImageWidth = 55;
        int ImageHeigh = 22;
        Font font = new Font("Arial", 12, FontStyle.Bold);
        Brush brush = new SolidBrush(Color.Black);//定义画笔,用于绘制文字             
        Bitmap BitImage = new Bitmap(ImageWidth, ImageHeigh);//创建一个图像       
        Graphics graphics = Graphics.FromImage(BitImage);//从图像获取一个绘画面       
        graphics.Clear(ColorTranslator.FromHtml("#F0F0F0")); //清除整个绘图画面并用颜色填充,这里从HTML代码获取color对象 
        drawLine(graphics, BitImage);
        RectangleF rect = new RectangleF(5, 2, ImageWidth, ImageHeigh);//定义文字的绘制矩形区域        
        //绘制验证码文字
        graphics.DrawString(CreateCheckCodeString(), font, brush, rect);
        drawPoint(BitImage);
        //保存图片为gif格式
        BitImage.Save(Response.OutputStream, ImageFormat.Gif);
        Response.ContentType = "image/gif";
        //Response.AddHeader("Content-Disposition", "inline;filename=1.gif");
        graphics.Dispose();//释放对象
        BitImage.Dispose();

    }
    private void drawLine(Graphics gfc, Bitmap img)
    {
        //选择画10条线,也可以增加,也可以不要线,只要随机杂点就行
        for (int i = 0; i < 10; i++)
        {
            int x1 = ran.Next(img.Width);
            int y1 = ran.Next(img.Height);
            int x2 = ran.Next(img.Width);
            int y2 = ran.Next(img.Height);
            gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);//注意画笔要淡,不然看不清
        }

    }

    private void drawPoint(Bitmap img)
    {
        int col = ran.Next();//在一次的图片中杂点颜色相同
        for (int i = 0; i < 100; i++)
        {
            int x = ran.Next(img.Width);
            int y = ran.Next(img.Height);
            img.SetPixel(x, y, Color.FromArgb(col));
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值