一個簡單的Silverlight驗證碼例子

來源:互聯網
上載者:User

在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(); //輸入焦點聚焦
     }
4、實現驗證效果,just try it!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.