不出现重复数字,带干扰的验证码

View Code
 1             Bitmap map = new Bitmap(200,80);
2 Graphics g = Graphics.FromImage(map);
3 g.Clear(Color.White);
4 Random r = new Random();
5 string codestr = null;
6 for (int i = 0; i < 4; i++)
7 {
8 string rdnum=r.Next(10).ToString ();
9 if (codestr != null)
10 {
11 if (codestr.IndexOf(rdnum) >= 0)
12 {
13 i--;
14 continue;
15 }
16 }
17 codestr += rdnum;
18 }
19 g.DrawString(codestr, new Font("宋体", 40), Brushes.Red, new PointF(8, 8));
20 //添加噪音线
21 for (int i = 0; i < 20; i++)
22 {
23 int x1 = r.Next(map.Width);
24 int y1 = r.Next(map.Height);
25 int x2 = r.Next(map.Width);
26 int y2 = r.Next(map.Height);
27 g.DrawLine(new Pen(Brushes.Plum), x1, y1, x2, y2);
28 }
29 //添加噪音点
30 for (int i = 0; i < 500; i++)
31 {
32 map.SetPixel(r.Next(map.Width), r.Next(map.Height), Color.FromArgb(r.Next(1)));
33 }
34 MemoryStream ms = new MemoryStream();
35 map.Save (ms,System .Drawing .Imaging .ImageFormat .Jpeg );
36 this .pictureBox1 .Image =Image .FromStream (ms);
posted @ 2012-02-10 11:49  志誠  阅读(230)  评论(2编辑  收藏  举报