C#生成验证码

(1)生成边框(即是大小)

 public void CreatYanZhengMa()                         @声明全局变量
        {
            this.panel1.Controls.Clear();                   bool bolStar=true;
                                                            Panel pyanzhengma = new Panel(); 
            pyanzhengma.Location = new Point(0, 0);         public  string yanzhengma = "";
            pyanzhengma.Size = new Size(84, 26);
            this.panel1.Controls.Add(pyanzhengma);
            yanzhengma = CreateRandomCode(4);
        }

(2)自定义方法(随机获取验证码)

private string CreateRandomCode(int codeCout)
        {
            string allChar = "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";
            string[] allCharArray = allChar.Split(',');
            string rangdomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeCout; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i*temp*((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(61);
                if (temp == t)
                {
                    return CreateRandomCode(codeCout);
                }
                temp = t;
                rangdomCode+=allCharArray[t];
            }
            return rangdomCode;
        }

(3)自定义方法(生成图片背景)

private void CretaeImage(string chenkCode,PaintEventArgs e)
        {
            if (chenkCode == null || chenkCode.Trim() == String.Empty)
                return;
            Bitmap image = new Bitmap(chenkCode.Length*15+10,24);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的前景噪音线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.BlueViolet, Color.Crimson, 1.2f, true);
                g.DrawString(chenkCode, font, brush, 2, 2);

                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Blue),0,0,image.Width-1,image.Height-1);

                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    image.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
                    e.Graphics.DrawImage(image,new Point(1,0));
                }
            }
            finally
            {
                bolStar = false;
                g.Dispose();
                image.Dispose();
            }
        }

(4)panle 单击事件

private void pyanzhengma_Click(object sender, EventArgs e)
        {
            if (bolStar == false)
            {
                bolStar = true;
                CreatYanZhengMa();
            }
        }

(5)自定义方法(触发生成图片事件)

 public void p_Paint(object sender, PaintEventArgs e)
        {
            CretaeImage(yanzhengma, e);
        }

(6)事件委托

public FRM_Login()
        {
            InitializeComponent();
            pyanzhengma.Paint += new PaintEventHandler(p_Paint);
            pyanzhengma.Click += new EventHandler(pyanzhengma_Click);
        }

编程结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值