加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

一个C#生成简单验证码的类

发布时间:2020-12-15 17:55:56 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using S

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Web.Security;
using System.Collections;
using System.Drawing;
 
public partial class Code : System.Web.UI.Page
{
    protected void Page_Load(object sender,EventArgs e)
    {
        if (Session["label"] == null)
        {
            string str = "";
            string[] code = {"0","1","2","3","4","5","6","7","8","9","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"};
 
            //随机产生字符串
            Random rand = new Random();
            for (int i = 0; i < 4; i++)
            {
                str = str + code[rand.Next(0,code.Length)];
            }
            CreateImage(str);
             
            foreach (var s in str)
            {
                 str += s;
            }
            Session["str"] = str;
            //foreach (string s in str)
            //{
            //    str += s;
            //}
            //Session["str"] = str;
          //  Label1.Text = str;
          //  Session["label"] = Label1.Text;
        }
    }
    //画验证图片
    private void CreateImage(string checkcode)
    {
        if (checkcode == null || checkcode.Length <= 0)
            return;
 
        int width = (int)(checkcode.Length * 25);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(checkcode.Length*32.5),40);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.White);
        //定义颜色
        Color[] c = { Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple };
        //定义字体
        string[] font = { "Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","宋体" };
        Random rand = new Random();
 
        //画图片的背景线
        for (int i = 0; i < 70; i++)
        {
            int x = rand.Next(image.Width);
            int y = rand.Next(image.Height);
            g.DrawLine(new Pen(Color.LightGray,1),x,y,10,20);
        }
        //输出不同字体和颜色的验证码字符
        for (int i = 0; i < checkcode.Length; i++)
        {
            int cindex = rand.Next(7);
            int findex = rand.Next(5);
 
            Font f = new System.Drawing.Font(font[findex],23,System.Drawing.FontStyle.Bold);//23表示字体大小
            Brush brush = new System.Drawing.SolidBrush(c[cindex]);
            int ii = 4;
            if ((i + 1) % 2 == 0)
            {
                ii = 2;
            }
            g.DrawString(checkcode.Substring(i,f,brush,15 + (i * 12),ii);
        }
 
        //画边框,画图片的边框线
        g.DrawRectangle(new Pen(Color.Black,0),image.Width - 1,image.Height - 1);
 
        //输出到浏览器
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
        HttpContext.Current.Response.ContentType = "image/jpeg";
        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();
    }
 
     
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读