随便验证码,

   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_
{
public partial class 数字母 : Form
{
public 数字母()
{
InitializeComponent();
随机验证码,在winform下边制作的当然也得以在web上边制作。}
//在WinForm窗体在贯彻自由验证码
private void pictureBox1_Click(object sender, EventArgs e)
{
//定义画布大小
Bitmap bmp = new Bitmap(100, 100);
//定义一个变量
string str =
“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;
Random r = new Random (); //随机生成
string newstr = “”;
for(int i=0;i<6;i++)
{
newstr += str[r.Next(0, 62)];
}
Graphics g = Graphics.FromImage(bmp);
//随机字体
String[] fname = { “黑体”, “燕书”, “金鼎文”, “仿宋”, “微软雅黑”,
“华文行楷” };
//随机颜色

Color[] color = { Color.Red, Color.Green, Color.Pink, Color.Yellow,
Color.Gold, Color.Black };
for(int i=0;i<newstr.Length;i++)
{
Point p = new Point(i * 15, 20);
g.DrawString(newstr[i].ToString(), new Font(fname[r.Next(0, 6)], 18,
FontStyle.Italic), new SolidBrush(color[r.Next(0, 6)]), p);
}
// 验证码上画图片的背景噪音线
for (int i=0;i<15;i++)
{
g.DrawLine(new Pen(color[r.Next(0, 6)]), new Point(r.Next(0, 100),
r.Next(0, 100)), new Point(r.Next(0, 100), r.Next(0, 100)));
}
// 验证码上画图片的背景噪音点
for (int i=0;i<300;i++)
{
bmp.SetPixel(r.Next(0, 100), r.Next(0, 100), color[r.Next(0, 6)]);
}
this.pictureBox1.Image = bmp;
}

using System; using
System.Collections.Generic; using System.ComponentModel; using
System.Data; using System.Drawing; using System.Linq; using System.Text;
using…

澳门金沙国际 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

首先建立一个windows应用窗体程序

namespace GDI_
{
public partial class 数字母 : Form
{
public 数字母()
{
InitializeComponent();
}
//在WinForm窗体在落到实处自由验证码
private void pictureBox1_Click(object sender, EventArgs e)
{
//定义画布大小
Bitmap bmp = new Bitmap(100, 100);
//定义一个变量
string str =
“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;
Random r = new Random (); //随机生成
string newstr = “”;
for(int i=0;i<6;i++)
{
newstr += str[r.Next(0, 62)];
}
Graphics g = Graphics.FromImage(bmp);
//随机字体
String[] fname = { “金鼎文”, “黑体”, “小篆”, “石籀文”, “微软雅黑”,
“华文行楷” };
//随机颜色

namespace GDI_
{
public partial class 数字母 : Form
{
public 数字母()
{
InitializeComponent();
}
//在WinForm窗体在落到实处自由验证码
private void pictureBox1_Click(object sender, EventArgs e)
{
//定义画布大小
Bitmap bmp = new Bitmap(100, 100);
//定义一个变量
string str =
“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;
Random r = new Random (); //随机生成
string newstr = “”;
for(int i=0;i<6;i++)
{
newstr += str[r.Next(0, 62)];
}
Graphics g = Graphics.FromImage(bmp);
//随机字体
String[] fname = { “金鼎文”, “甲骨文”, “陶文”, “金鼎文”, “微软雅黑”,
“华文行楷” };
//随机颜色

在winform窗体上添加一个button控件和picturebox控件

Color[] color = { Color.Red, Color.Green, Color.Pink, Color.Yellow,
Color.Gold, Color.Black };
for(int i=0;i<newstr.Length;i++)
{
Point p = new Point(i * 15, 20);
g.DrawString(newstr[i].ToString(), new Font(fname[r.Next(0, 6)], 18,
FontStyle.Italic), new SolidBrush(color[r.Next(0, 6)]), p);
}
// 验证码上画图纸的背景噪音线
for (int i=0;i<15;i++)
{
g.DrawLine(new Pen(color[r.Next(0, 6)]), new Point(r.Next(0, 100),
r.Next(0, 100)), new Point(r.Next(0, 100), r.Next(0, 100)));
}
// 验证码上画图片的背景噪音点
for (int i=0;i<300;i++)
{
bmp.SetPixel(r.Next(0, 100), r.Next(0, 100), color[r.Next(0, 6)]);
}
this.pictureBox1.Image = bmp;
澳门金沙国际 ,}

Color[] color = { Color.Red, Color.Green, Color.Pink, Color.Yellow,
Color.Gold, Color.Black };
for(int i=0;i<newstr.Length;i++)
{
Point p = new Point(i * 15, 20);
g.DrawString(newstr[i].ToString(), new Font(fname[r.Next(0, 6)], 18,
FontStyle.Italic), new SolidBrush(color[r.Next(0, 6)]), p);
}
// 验证码上画图片的背景噪音线
for (int i=0;i<15;i++)
{
g.DrawLine(new Pen(color[r.Next(0, 6)]), new Point(r.Next(0, 100),
r.Next(0, 100)), new Point(r.Next(0, 100), r.Next(0, 100)));
}
// 验证码上画图片的背景噪音点
for (int i=0;i<300;i++)
{
bmp.SetPixel(r.Next(0, 100), r.Next(0, 100), color[r.Next(0, 6)]);
}
this.pictureBox1.Image = bmp;
}

下边就是代码的写入

澳门金沙国际 2

 

俺们所见到的上边的验证码的每当你点击验证码的时候它就会刷新新的验证码出来

本来大家那里是点击button的时候刷新新的验证码

pictureBox是用来呈现验证码的

button中的代码如下

1  .首先是将验证码中的数字拉长到一个字符串当中

2  .其次突显在picturebox那个 控件上边

private void button1_Click(object sender, EventArgs e)
        {

            Random r = new Random();        //首先建立一个无限制挑选数字
            string str = null;                            
//建立一个空的字符串来储存验证码
            for(int i=0;i<5;i++)                        
//通过一个for循环来讲数字有增无减到字符串当中
            {
                int number = r.Next(0, 10);
                str += number;
            }
            //MessageBox.Show(str);
            Bitmap bmp = new Bitmap(120,
20);//建立展现验证码的宽高的限定
            Graphics g = Graphics.FromImage(bmp);           
            for (int i = 0; i < 5; i++)
            {
                Point p = new Point(i*10,0);
                string[] font1 = { “微雅软黑”, “大篆”,
“大篆”,”宋体”,”华文彩云” };
                Color[] color1 = { Color.Red, Color.Black,
Color.Yellow, Color.Purple, Color.Pink};
                g.DrawString(str[i].ToString(),new Font (font1
[r.Next (0,5)],20 ,FontStyle.Bold),new SolidBrush
(color1[r.Next(0,5)]),p);//将验证码画出来并肆意的挑三拣四颜色字体跟地点(地点point在上头确定了高 
妹每个验证码相隔i*10个像素)
            }

//下边是在验证码的图像上边添加线条
            for(int i=0;i<15;i++)
            {
                Point p = new Point(r.Next(0, bmp.Width), r.Next(0,
bmp.Height));//线条添加的两点的确定
                Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0,
bmp.Height));
                g.DrawLine(new Pen(Brushes.Green), p, p1);
            }
            pictureBox1.Image = bmp;   //将图像浮现到picturebox上边
        }

下边是搞活的显得

澳门金沙国际 3

 

相关文章