winRT 圖片驗證碼生成


今天糾結了半天這個問題,windows phone 采用下面方法進行畫圖,原理很簡單,主要是先創建一個Grid當畫板,在其上面添加各種控件,組成一個類似驗證碼的圖形,然后再轉成圖片格式,在UI層綁定Iamge 控件。

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 = 40;//r.Next(h - 3, h);
                 t.FontWeight = FontWeights.Bold;
                 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;
         }

但是發現RT上WriteableBitmap類已經沒有下面的構造函數

 public WriteableBitmap(UIElement element, Transform transform);


也就是說上面的Grid已經找不到方法轉成圖片。我尋找了三方畫圖控件庫(http://writeablebitmapex.codeplex.com/),發現也不支持畫文字、數字。

正在糾結時,換個角度柳暗花明啊。

客戶端為何不用圖片,直接綁定Grid

UI層用:

<ContentControl HorizontalAlignment="Left" Width="120" Height="50" Content="{Binding CheckCodeImage,Mode=TwoWay}" ></ContentControl>

 

希望對你有用!

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM