Silverlight中驗證碼產生

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   sp   for   on   

   public class ValidationCode    {        Random r = new Random(DateTime.Now.Millisecond);        /// <summary>        /// 用於在產生驗證碼        /// </summary>        /// <param name="text">產生驗證碼的字串</param>        /// <param name="img">silverlight中的Imge控制項</param>        public void CreatImage(string text, Image img)         {            int height =Convert.ToInt32(img.Height);            int width = Convert.ToInt32(img.Width);            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() * width,                        r.NextDouble() * height));                }                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);            }            Gx.Children.Add(cv1);            //用於產生數字            Canvas cv2 = new Canvas();            int y = 0;            int lw = 6;            double w = (width - lw) / text.Length;            int h = (int)height;            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); //返回一個指定範圍內的數  設定字型大小<像素高度-3>                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(cv2);            //基於記憶體管理映像的類            WriteableBitmap W = new WriteableBitmap(Gx, new TransformGroup());            W.Render(Gx, new TransformGroup());            img.Source = W;        }
}


 

Silverlight中驗證碼產生

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.