C# 验证码

    protected void Page_Load(object sender, EventArgs e)
    {
        string key="";
        byte[] data = GenerateVerifyImage(4, ref key);
        Response.OutputStream.Write(data,0,data.Length);
    }

    public byte[] GenerateVerifyImage(int nLen, ref string strKey)
    {
        int nBmpWidth = 13 * nLen + 5;
        int nBmpHeight = 25;
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth, nBmpHeight);

        // 1. 生成随机背景颜色
        int nRed, nGreen, nBlue;  // 背景的三元色
        System.Random rd = new Random((int)System.DateTime.Now.Ticks);
        nRed = rd.Next(255) % 128 + 128;
        nGreen = rd.Next(255) % 128 + 128;
        nBlue = rd.Next(255) % 128 + 128;

        // 2. 填充位图背景
        System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
        graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue))
         , 1
         , 1
         , nBmpWidth
         , nBmpHeight);

        graph.DrawRectangle(new Pen(Color.Black, 1), 0, 0, nBmpWidth - 1, nBmpHeight - 1);



        // 3. 绘制干扰线条,采用比背景略深一些的颜色
        int nLines = 8;
        System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
        for (int a = 0; a < nLines; a++)
        {
            int x1 = rd.Next(nBmpWidth/9, nBmpWidth/5);
            int y1 = rd.Next(2, nBmpHeight-2);
            int x2 = rd.Next(nBmpWidth / 6, nBmpWidth-2);
            int y2 = rd.Next(2, nBmpHeight - 2);
            graph.DrawLine(pen, x1, y1, x2, y2);
        }


        //绘制干扰点
        //graph.dr

        int points = 10;
        for (int i = 1; i <= points; i++)
        {
            int p1 = rd.Next(2, nBmpWidth - 2);
            int p2 = rd.Next(2, nBmpHeight - 2);
            //graph.DrawEllipse(new Pen(Color.Black,1),p1,p2,1,1);
            graph.FillEllipse(new SolidBrush(Color.Black), p1, p2, 1, 2);
        }



        // 采用的字符集,可以随即拓展,并可以控制字符出现的几率
        string strCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        // 4. 循环取得字符,并绘制
        string strResult = "";
        for (int i = 0; i < nLen; i++)
        {
            int x = (i * 13 + rd.Next(3));
            int y = rd.Next(4) + 1;

            // 确定字体
            System.Drawing.Font font = new System.Drawing.Font("Courier New",
             12 + rd.Next() % 4,
             System.Drawing.FontStyle.Bold);
            char c = strCode[rd.Next(strCode.Length)];  // 随机获取字符
            strResult += c.ToString();

            // 绘制字符
            graph.DrawString(c.ToString(),
             font,
             new SolidBrush(System.Drawing.Color.FromArgb(nRed - 100 + y * 3, nGreen - 100 + y * 3, nBlue - 80 + y * 3)),
             x,
             y);
        }
        graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        // 5. 输出字节流
        System.IO.MemoryStream bstream = new System.IO.MemoryStream();
        bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);
        bmp.Dispose();
        graph.Dispose();

        strKey = strResult;
        byte[] byteReturn = bstream.ToArray();
        bstream.Close();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值