A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 ♠Akechi♠ 于 2014-3-23 14:47 编辑

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();

                // 利用RenderTargetBitmap对象,以保存图片
                RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)this.imgAuthCode.Width, (int)this.imgAuthCode.Height, 96, 96, PixelFormats.Pbgra32);
                renderBitmap.Render(drawingVisual);
                ImageSource source = BitmapFrame.Create(renderBitmap);
                imgAuthCode.Source = source;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            intCode();
        }

        private void imgAuthCode_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            intCode();
        }




3 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
额,我用Winform做的,分享下
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;

  10. namespace 验证码2
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         private void pictureBox1_Click(object sender, EventArgs e)
  19.         {
  20.             Bitmap yanzheng=new Bitmap(200,70);//声明一个宽200,高70的位图
  21.             Graphics g = Graphics.FromImage(yanzheng);//新建个绘图的对象
  22.             Random n=new Random();//随机5个数
  23.             string str = null;
  24.             string[] font = { "黑体", "华文行楷", "微软雅黑", "隶书", "幼圆", "宋体", "华文彩文" };
  25.             Color[] col = { Color.Green, Color.Blue, Color.Red, Color.Black, Color.BlueViolet, Color.GreenYellow, Color.Orange };
  26.             for (int i = 0; i < 5; i++)//5个随机的数字
  27.                         {
  28.                 str+=n.Next(0,10).ToString();
  29.                         }
  30.             //位图,画线,画点,随机字体,随机颜色
  31.             for (int i = 0; i < 100; i++)//噪线
  32.             {
  33.                 Point p1=new Point(n.Next(0,yanzheng.Width),n.Next(0,yanzheng.Height));//两点一线,这是第一个点的随机坐标
  34.                 Point p2=new Point(n.Next(0,yanzheng.Width),n.Next(0,yanzheng.Height));//这是第二个点的随机坐标,都控制在位图的宽和高之内
  35.                 Pen pen=new Pen(col[n.Next(0,7)],5);
  36.                 g.DrawLine(pen,p1,p2);
  37.             }
  38.             for (int i = 0; i < 200; i++)//噪点
  39.                         {
  40.                  Point p=new Point(n.Next(0,yanzheng.Width),n.Next(0,yanzheng.Height));
  41.                  yanzheng.SetPixel(p.X, p.Y, col[n.Next(0, 7)]);
  42.                         }
  43.             for (int i = 0; i < 5; i++)
  44.                         {
  45.                 Point p=new Point(i*30,n.Next(0,6)*5);//遍历的坐标,每一个坐标的宽度会加30,高度随机
  46.                            g.DrawString(str[i].ToString(),new Font(font[n.Next(0,7)],40),new SolidBrush(col[n.Next(0,7)]),p);//把文本写进位图内,随机颜色,随机字体
  47.                         }
  48.             pictureBox1.Image = yanzheng;//最后把这个位图赋值给图片
  49.         }
  50.     }
  51. }
复制代码

附上图,点击图片更换验证码

11.png (32.38 KB, 下载次数: 28)

11.png
回复 使用道具 举报
cancle 发表于 2014-3-23 21:15
额,我用Winform做的,分享下
附上图,点击图片更换验证码

你这个太炫了0.0,绝对是艺术家···我那个很朴实···因为我不会API,刚学
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马