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>

 

希望对你有用!

 

 

 

posted @ 2013-02-20 17:14  龙月  阅读(1661)  评论(9编辑  收藏  举报