WPF的图形接口是API的,学了相关一些知识,自己画了个验证码,不知道有没有更简洁的方法:string code;
protected void intCode()
{
Random random = new Random();
code = Convert.ToString(random.Next(1000, 9999));
for (int i = 0; i < code.Length; i++)
{
//设置下StackPanel的背景
spHouse.Background = new SolidColorBrush(Color.FromArgb(150, 230, 230, 230));
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawText(
new FormattedText(Convert.ToString(code[0]), CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Green),
new Point(15, 8));
drawingContext.DrawText(
new FormattedText(Convert.ToString(code[1]), CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Blue),
new Point(30, 8));
drawingContext.DrawText(
new FormattedText(Convert.ToString(code[2]), CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Red),
new Point(45, 8));
drawingContext.DrawText(
new FormattedText(Convert.ToString(code[3]), CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.DeepSkyBlue),
new Point(60, 8));
drawingContext.Close();