如何使用中文验证符
1 起始:
2 public void Start_Create()
3 {
4 Encoding gb = Encoding.GetEncoding("gb2312");
5 object[] bytes = CreateRegionCode(4);
6 string str1=gb.GetString((byte[]) Convert.ChangeType(bytes[0], typeof(byte[])));
7 string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
8 string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
9 string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
10 string str = str1+" "+str2+" "+str3+" "+str4;
11 CreateImage(str);
12 }
13
14 public object[] CreateRegionCode(int strLength)
15 {
16 string[] rBase = new string[16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
17 Random rnd = new Random();
18 object[] bytes = new object[strLength];
19 for(int i=0;i<strLength;i++)
20 {
21 //区位码第一位
22 int r1 = rnd.Next(11,14);
23 string str_r1 = rBase[r1].Trim();
24 //区位码第二位
25 rnd = new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);
26 int r2;
27 if (r1==13)
28 { r2=rnd.Next(0,7);
29 }
30 else
31 { r2=rnd.Next(0,16);
32 }
33 string str_r2=rBase[r2].Trim();
34 //区位码第三位
35 rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
36 int r3=rnd.Next(10,16);
37 string str_r3=rBase[r3].Trim();
38 //区位码第四位
39 rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
40 int r4;
41 if (r3==10)
42 { r4=rnd.Next(1,16);
43 }
44 else if (r3==15)
45 { r4=rnd.Next(0,15);
46 }
47 else
48 { r4=rnd.Next(0,16);
49 }
50 string str_r4=rBase[r4].Trim();
51 //存储随机产生的汉字区位码
52 byte byte1=Convert.ToByte(str_r1 + str_r2,16);
53 byte byte2=Convert.ToByte(str_r3 + str_r4,16);
54 byte[] str_r=new byte[]{byte1,byte2};
55 bytes.SetValue(str_r,i);
56 }
57 return bytes;
58 }
59
60 public void CreateImage(string str1)
61 {
62 int iWidth = (int)(str1.Length*18);
63 System.Drawing.Bitmap image = new Bitmap(iWidth,30);
64 Graphics g = Graphics.FromImage(image);
65
66 Font f = new System.Drawing.Font("楷体", 12, System.Drawing.FontStyle.Bold);
67 Brush b = new System.Drawing.SolidBrush(Color.White);
68 g.FillRectangle(new System.Drawing.SolidBrush(Color.BlueViolet),0,0,image.Width,image.Height);
69
70 //g.Clear(Color.BlueViolet);
71 g.DrawString(str1, f, b,15, 5);
72 Random rnd = new Random();
73 for(int i=0;i<20;i++)
74 {
75 g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),rnd.Next(image.Width),0,1,image.Height);
76 }
77 //
78 // Pen blackPen = new Pen(Color.Black, 0);
79 // Random rand = new Random();
80 // for (int i=0;i<5;i++)
81 // {
82 // int y = rand.Next(image.Height);
83 // g.DrawLine(blackPen,0,y,image.Width,y-5);
84 // }
85
86
87 System.IO.MemoryStream ms = new System.IO.MemoryStream();
88 image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
89 Response.ClearContent();
90 Response.ContentType = "image/Jpeg";
91 Response.BinaryWrite(ms.ToArray());
92 g.Dispose();
93 image.Dispose();
94
95 }
2 public void Start_Create()
3 {
4 Encoding gb = Encoding.GetEncoding("gb2312");
5 object[] bytes = CreateRegionCode(4);
6 string str1=gb.GetString((byte[]) Convert.ChangeType(bytes[0], typeof(byte[])));
7 string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
8 string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
9 string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
10 string str = str1+" "+str2+" "+str3+" "+str4;
11 CreateImage(str);
12 }
13
14 public object[] CreateRegionCode(int strLength)
15 {
16 string[] rBase = new string[16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
17 Random rnd = new Random();
18 object[] bytes = new object[strLength];
19 for(int i=0;i<strLength;i++)
20 {
21 //区位码第一位
22 int r1 = rnd.Next(11,14);
23 string str_r1 = rBase[r1].Trim();
24 //区位码第二位
25 rnd = new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);
26 int r2;
27 if (r1==13)
28 { r2=rnd.Next(0,7);
29 }
30 else
31 { r2=rnd.Next(0,16);
32 }
33 string str_r2=rBase[r2].Trim();
34 //区位码第三位
35 rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
36 int r3=rnd.Next(10,16);
37 string str_r3=rBase[r3].Trim();
38 //区位码第四位
39 rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
40 int r4;
41 if (r3==10)
42 { r4=rnd.Next(1,16);
43 }
44 else if (r3==15)
45 { r4=rnd.Next(0,15);
46 }
47 else
48 { r4=rnd.Next(0,16);
49 }
50 string str_r4=rBase[r4].Trim();
51 //存储随机产生的汉字区位码
52 byte byte1=Convert.ToByte(str_r1 + str_r2,16);
53 byte byte2=Convert.ToByte(str_r3 + str_r4,16);
54 byte[] str_r=new byte[]{byte1,byte2};
55 bytes.SetValue(str_r,i);
56 }
57 return bytes;
58 }
59
60 public void CreateImage(string str1)
61 {
62 int iWidth = (int)(str1.Length*18);
63 System.Drawing.Bitmap image = new Bitmap(iWidth,30);
64 Graphics g = Graphics.FromImage(image);
65
66 Font f = new System.Drawing.Font("楷体", 12, System.Drawing.FontStyle.Bold);
67 Brush b = new System.Drawing.SolidBrush(Color.White);
68 g.FillRectangle(new System.Drawing.SolidBrush(Color.BlueViolet),0,0,image.Width,image.Height);
69
70 //g.Clear(Color.BlueViolet);
71 g.DrawString(str1, f, b,15, 5);
72 Random rnd = new Random();
73 for(int i=0;i<20;i++)
74 {
75 g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),rnd.Next(image.Width),0,1,image.Height);
76 }
77 //
78 // Pen blackPen = new Pen(Color.Black, 0);
79 // Random rand = new Random();
80 // for (int i=0;i<5;i++)
81 // {
82 // int y = rand.Next(image.Height);
83 // g.DrawLine(blackPen,0,y,image.Width,y-5);
84 // }
85
86
87 System.IO.MemoryStream ms = new System.IO.MemoryStream();
88 image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
89 Response.ClearContent();
90 Response.ContentType = "image/Jpeg";
91 Response.BinaryWrite(ms.ToArray());
92 g.Dispose();
93 image.Dispose();
94
95 }
浙公网安备 33010602011771号