Silverlight验证码生成

  • 在Silverlight的登录页面中经常用到输入验证码,怎么生成的呢,其实和.NET生成差不多,以下简单介绍一种:

    1、在Silverlight项目下建立一个类IndentifyCodeClass.cs文件

        1)、首先注意添加引用 using System.Windows.Media.Imaging;

        2)、添加一个用来产生验证码的方法
            public string CreateIndentifyCode(int count)
            {
                string allchar = "1,2,3,4,5,6,7,8,9,0,A,a,B,b,C,c,D,d,E,e,F,f," +
                    "G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s," +
                    "T,t,U,u,V,v,W,w,X,x,Y,y,Z,z";
                string[] allchararray = allchar.Split(',');
                string randomcode = "";
                int temp = -1;
                Random rand = new Random();
                for (int i = 0; i < count; i++)
                {
                    if (temp != -1)
                    {
                        rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                    }
                    int t = rand.Next(61);
                    if (temp == t)
                    {
                        return CreateIndentifyCode(count);
                    }
                    temp = t;
                    randomcode += allchararray[t];
                }
                return randomcode;
            }

        3)、添加一个方法,用来产生验证码的背景噪声图片
            Random r = new Random(DateTime.Now.Millisecond);
            public void CreatImage(string Text, Image imgsource, int iw, int ih)
            {
                Grid Gx = new Grid();
                Canvas cv1 = new Canvas();
                for (int i = 0; i < 6; i++)
                {
                    Polyline p = new Polyline();
                    for (int ix = 0; ix < r.Next(3, 6); ix++)
                    {
                        p.Points.Add(new Point(r.NextDouble() * iw,
                            r.NextDouble() * ih));
                    }
                    byte[] Buffer = new byte[3];
                    r.NextBytes(Buffer);
                    SolidColorBrush SC = new SolidColorBrush(Color.FromArgb(255,
                        Buffer[0], Buffer[1], Buffer[2]));
                    p.Stroke = SC;
                    p.StrokeThickness = 0.5;
                    cv1.Children.Add(p);
                }
                Canvas cv2 = new Canvas();
                int y = 0;
                int lw = 6;
                double w = (iw - lw) / Text.Length;
                int h = (int)ih;
                foreach (char x in Text)
                {
                    byte[] Buffer = new byte[3];
                    r.NextBytes(Buffer);
                    SolidColorBrush SC = new SolidColorBrush(Color.FromArgb(255,
                        Buffer[0], Buffer[1], Buffer[2]));
                    TextBlock t = new TextBlock();
                    t.TextAlignment = TextAlignment.Center;
                    t.FontSize = r.Next(h - 3, h);
                    t.Foreground = SC;
                    t.Text = x.ToString();
                    t.Projection = new PlaneProjection()
                    {
                        RotationX = r.Next(-30, 30),
                        RotationY = r.Next(-30, 30),
                        RotationZ = r.Next(-10, 10)
                    };
                    cv2.Children.Add(t);
                    Canvas.SetLeft(t, lw / 2 + y * w);
                    Canvas.SetTop(t, 0);
                    y++;
                }
                Gx.Children.Add(cv1);
                Gx.Children.Add(cv2);
                WriteableBitmap W = new WriteableBitmap(Gx, new TransformGroup());
                W.Render(Gx, new TransformGroup());
                imgsource.Source = W;
            }

    2、在登录页面的UserControl_Loaded事件中添加如下代码即可:

          IndentifyCodeClass code = new IndentifyCodeClass();

          string generatedCode =  code.CreateIndentifyCode(6);
          code.CreatImage(generatedCode , CheckCodeImage, 150, 30);

    3、获取用户在文本框中输入的验证码,然后和产生的验证码进行比较验证。

       ....

         string indentifyCode = this.tbIndentifyCode.Text.trim();

         if(generatedCode !=indentifyCode)

         {

              MessageBox.Show("验证码输入错误!");

              this.tbIndentifyCode.Focus(); //输入焦点聚焦

         }

问题: 3中的generatedCode是局部变量


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值