标签:
主要功能:点击验证码可更换,输入验证码进行登陆
需要导入命名空间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 }
到这里效果如下:
这时候我们在加入一些线条:
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 }
大致就这样了,没什么技术含量!
源码地址:http://pan.baidu.com/s/1gdgYqpt
标签:
原文地址:http://www.cnblogs.com/MaskO1/p/GDI.html