Winform之GDI绘制验证码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Winform之GDI绘制验证码

Winform之GDI绘制验证码

 2015/4/24 21:06:42  MaskO1  程序员俱乐部  我要评论(0)
  • 摘要:主要功能:点击验证码可更换,输入验证码进行登陆需要导入命名空间System.Drawing;产生五位的随机字符串:1Randomrandom=newRandom();2//产生5个随机字符串3str=null;4for(inti=0;i<5;i++)5{6intj=random.Next(64,90);7str=str+(char)j;8}画字符串1//创建图片2Bitmapbmp=newBitmap(120,30);3//创建GDI对象4Graphicsg=Graphics
  • 标签:for winform

主要功能:点击验证码可更换,输入验证码进行登陆

 

需要导入命名空间System.Drawing;

产生五位的随机字符串:

1        Random random = new Random();
2             //产生5个随机字符串
3             str = null;
4             for (int i = 0; i < 5; i++)
5             {
6                 int j = random.Next(64, 90);
7                 str = str + (char)j;
8             }

画字符串

 1             //创建图片
 2             Bitmap bmp = new Bitmap(120,30);
 3             //创建GDI对象
 4             Graphics g = Graphics.FromImage(bmp);
 5             //循环画字符串
 6             for (int i = 0; i < str.Length; i++)
 7             {
 8                 Point p = new Point(i*20,0);
 9                 //随机字体
10                 string[] fonts = { "宋体", "黑体", "微软雅黑", "隶属", "仿宋", };
11                 //随机颜色
12                 Color[] colors = { Color.Red, Color.Aquamarine, Color.Blue, Color.Yellow, Color.YellowGreen };
13                 //画字符串
14                 g.DrawString(str[i].ToString(), new Font(fonts[random.Next(0, fonts.Length)], 20, FontStyle.Bold), new SolidBrush(colors[random.Next(0, colors.Length)]), p);
15             }                

到这里效果如下:

QQ38BYugvQAz9BYihvwAx9Bcghv4CxNBfgBj6CxBDfwFi6C9ADH//DSBGv/4+9fcXnn1+/4svv6G/AJXo19/Dhw9/+umnS0tL+gtQCf0FiKG/ADH0FyCG/gLE0F+AGPoLEEN/AWLoL0AM/QWIob8AMfQXIIb+AsTQX4AY+gsQQ38BYugvQA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3yRJjH8BqqK/ADH0FyCG/gLE0F+AGIOvf9BfgKroL0AM8w8AMXz+DSCG/gLE0F+AGPoLEEN/AWLoL0AM/QWIob8AMfQXIIb+AsTQX4AY+gsQQ38BYugvQAz9BYihvwAx9Bcghv4CxNBfgBj6CxBDfwFi6C9ADP0FiLHu/gJQlnX0F4BK6S9ADP0FiNFsNo8cOfLZZ58tLy/rL8DmaTab8/Pzn3/++fLy8v8BCFbfXg6NBNcAAAAASUVORK5CYII=" alt="" />

这时候我们在加入一些线条:

1        for (int i = 0; i < 20; i++)
2             {
3                 Point p1 = new Point(random.Next(0, bmp.Width), random.Next(0, bmp.Height));
4                 Point p2 = new Point(random.Next(0, bmp.Width), random.Next(0, bmp.Height));
5                 g.DrawLine(new Pen(Color.Green), p1, p2);
6             }

在画一些像素点:

1            //画像素点
2             for (int i = 0; i < 500; i++)
3             {
4                 Point p = new Point(random.Next(0, bmp.Width), random.Next(0, bmp.Height));
5                 bmp.SetPixel(p.X, p.Y, Color.Black);
6             }                

RSS4+lSV1fMtam+Tt9RPmfoacLs8cBpZRSq/k1ple3nOe+5FvOziysHMtb/9iy0vsvHqTnW8AESC1mizOyW7DGgvlyczIhPHubbmFMaOqFaQpLDkAplY9tzw4XLp8+Pjh/bHU++9IYYOqrWDAeE8wHigVVMPJ2SKWpTa+xMJjtel4uC90II5eA1GJ2GYNCO6nG8kFoTGdmLlvSzWq4mpd71AFkA9usdErpIW1hs2L1lo2Ou6Nalc8eLJQedpIkR9bzGYyu5yUv72VLbknveWikdmoeffkyJ+lT6MY36tm3UpsvN0e7nhlbZWyxbj3L7mazCtby0JHo2dLsGf36kmczo4DmoZqPFd75Cuu69MNJXvnTbl7di7uxtnYYukZopHZqHn358gCSSlKbuB9YMGYPsg18EwvG2mHj/BMFTrz8h5FPmB5bnT+yfv744OzRBedtBt1hQ93XrFQxtYmvtva31jUmPbpp688eXRg97TDie6kwcTGzSO3UkFppI1Kbje+MOdlCefUZ/Xr+cESpWY1zr8KcwA1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UNqm6321G7n0XTTaaXWVeMxkFq0DKlttiiK/lWHkvcVpNJtyvdQy5GkSC1ahtQ2WxRF/2wjUouWIbXNRmqBRiC1zRZF0eU2IrVoGVLbbKQWaARS22xy/2uvqZv2TQvUidQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoA4UgsA4kgtAIgjtQAgjtQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoA4UgsA4kgtAIgjtQAgjtQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoA4UgsA4kgtAIgjtQAgjtQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoA4UgsA4kgtAIgjtQAgjtQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoA4UgsA4kgtAIgjtQAgjtQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoA4UgsA4kgtAIgjtQAgjtQCgDhSCwDiSC0AiCO1ACCO1AKAOFILAOJILQCII7UAII7UAoC4stReAADUJJhadwUAoC6kFgDEkVoAEBfH8ebmJqkFAEFxHG9tbZFaABAUx/H29japBQBBcRzv7OyoGAAgKUkSdQkAIGBzc3Nra2t7e3tnZ+f/KvDnqjShtlYAAAAASUVORK5CYII=" alt="" />

大致就这样了,没什么技术含量!

源码地址:http://pan.baidu.com/s/1gdgYqpt

上一篇: jsonj解析网络数据 下一篇: 没有下一篇了!
发表评论
用户名: 匿名