Silverlight 随机验证码生成图片
1 var _IndextifyCode=IndentifyCode.Instance.CreateIndentifyCode(4); 2 3 IndentifyCode.Instance.CreateImage(_IndextifyCode,"image名称",100,35);//100,35大小 4 5 public class IndentifyCode 6 { 7 private static IndentifyCode instance = null; 8 public static IndentifyCode Instance 9 { 10 get 11 { 12 return instance ?? (instance = new IndentifyCode()); 13 } 14 } 15 16 17 18 Random r = new Random(DateTime.Now.Millisecond); 19 20 21 22 23 24 #region 产生随机数 25 public string CreateIndentifyCode(int length) 26 { 27 //由于1,I,l,O,o,0容易混淆,则不显示 28 string allchar = "2,3,4,5,6,7,8,9,A,a,B,b,C,c,D,d,E,e,F,f," + 29 "G,g,H,h,i,J,j,K,k,L,M,m,N,n,P,p,Q,q,R,r,S,s," + 30 "T,t,U,u,V,v,W,w,X,x,Y,y,Z,z"; 31 string[] allchararray = allchar.Split(','); 32 string randomcode = ""; 33 int temp = -1; 34 Random rand = new Random(); 35 for (int i = 0; i < length; i++) 36 { 37 if (temp != -1) 38 { 39 rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); 40 } 41 int t = rand.Next(allchararray.Length); 42 if (temp == t) 43 { 44 return CreateIndentifyCode(length); 45 } 46 temp = t; 47 randomcode += allchararray[t]; 48 } 49 return randomcode; 50 } 51 #endregion 52 53 }