C#winform随机图片验证码pictureBox

小泽 2019-12-16 07:36 0条评论
         void Button1Click(object sender, EventArgs e)//验证码
        {
            Random sj = new Random();//置随机数字
            string sz = null;
            for(int i = 0;i <1;i++) 
            {
                int jg = sj.Next(0,9);
                sz +=  jg;
            string xx =  "q|w|e|r|t|y|u|i|o|p|l|k|j|h|g|f|d|s|a|m|n|b|v|c|x|z"; 
            string dx =  "M|N|B|V|C|X|Z|A|S|D|F|G|H|J|K|L|P|O|I|U|Y|T|R|E|W|Q";
            string zw = "爱|爱|分|享|资|源|网|爱|分|享|资|源|网|爱|分|享|资|源|网|爱|分|享|资|源|网|爱";
            string[] fg = xx.Split('|');
            string[] fg1 = dx.Split('|');
            string[] fg2 = zw.Split('|');
            int fgs = sj.Next(0,26);
            sz= fg[fgs]+sz+fg1[fgs]+fg2[fgs];
            }
            //创建对象
            Bitmap bmp = new Bitmap(90,30);
            Graphics g = Graphics.FromImage(bmp);
            for(int i=0; i<4;i++)
            {
                Point p = new Point(i*20,0);
                string[] fonts = { "微软雅黑", "宋体", "黑体", "楷体" };
                Color[] colors = { Color.BlueViolet, Color.Blue, Color.Brown, Color.Black};    
                g.DrawString(sz[i].ToString(), new Font(fonts[sj.Next(0, 4)], 17, FontStyle.
                Underline),new SolidBrush(colors[sj.Next(0, 4)]), p);
            }
            for (int i = 0; i < 5; i++)//斜线
            {
                Point p1=new Point(sj.Next(0,bmp.Width),sj.Next(0,bmp.Height));
                Point p2=new Point(sj.Next(0,bmp.Width),sj.Next(0,bmp.Height));
                g.DrawLine(new Pen(Brushes.AliceBlue), p1, p2);
            }
            for (int i = 0; i < 100; i++)//底图杂色
            {
                Point p=new Point(sj.Next(0,bmp.Width),sj.Next(0,bmp.Height));
                bmp.SetPixel(p.X, p.Y, Color.Blue);
            }

            pictureBox1.Image = bmp;
            
        }
image.pngimage.png


本文标签: C#, winform, 验证, 随机, 验证码, 图片, pictureBox
0.0

说点什么吧
  • 全部评论(0
    还没有评论,快来抢沙发吧!