GDI+在绘制验证码中的使用

GDI+最简单的理解就是用来绘图的。其中包括点、直线、矩形、字符串等等。

先简单来个例子,说明如何在winform窗体中绘制一条直线,并且这条直线不随着窗体的移动而消失。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GDIDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //一根笔 一张纸 两点 绘制直线的对象
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建一个GDI对象
            Graphics g = this.CreateGraphics();//new Graphics();
      
            //创建一支画笔对象
            Pen pen = new Pen(Brushes.Yellow);
            
            //创建两个点
            Point p1 = new Point(30, 50);
            Point p2 = new Point(250, 250);

            g.DrawLine(pen, p1, p2);
        }

        /// <summary>
        /// 重新绘制窗体的时候,直线也重新画一遍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建一个GDI对象
            Graphics g = this.CreateGraphics();//new Graphics();

            //创建一支画笔对象
            Pen pen = new Pen(Brushes.Yellow);

            //创建两个点
            Point p1 = new Point(30, 50);
            Point p2 = new Point(250, 250);

            g.DrawLine(pen, p1, p2);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Brushes.Yellow);
            Size size=new System.Drawing.Size(80,120);
            Rectangle rec = new Rectangle(new Point(50,50),size);
            g.DrawRectangle(pen, rec);
        }
    }
}
GDI绘制直线

那么有了基本的了解就可以用类似的方法来绘制验证码了。主要包括绘制字符串、直线、点三部分,在BitMap位图上面绘制,然后将整个图片镶嵌到PictureBox中,单击该控件爱你,验证码发生变化。具体代码如下

 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.Windows.Forms;
 9 
10 namespace 点击生成验证码
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17         }
18 
19         /// <summary>
20         /// 点击更换验证码
21         /// </summary>
22         /// <param name="sender"></param>
23         /// <param name="e"></param>
24         private void pictureBox1_Click(object sender, EventArgs e)
25         {
26             Random r = new Random();
27             string str = null;
28             for (int i = 0; i < 5; i++)
29             {
30                 int rNumber = r.Next(0, 10); //产生5个验证码随机数
31                 str += rNumber.ToString();
32 
33             }
34 
35             Bitmap bmp = new Bitmap(150, 120);
36            //创建一个GDI对象
37             Graphics g = Graphics.FromImage(bmp); //可以理解为将位图作为画布
38 
39             //绘制验证码上面的数字
40             for (int i = 0; i < 5; i++)
41             {
42                 Point p=new Point(i*20,0); //此处的相对位置,不是相对窗体的,而是相对于pictureBox控件的 
43                 string[] font = {"微软雅黑","宋体","黑体","隶书","仿宋" };
44                 Color[] colors = { Color.Yellow, Color.Blue, Color.Black, Color.Red, Color.Green };
45                 
46                 g.DrawString(str[i].ToString(), new Font(font[r.Next(0,5)],20,FontStyle.Bold),new SolidBrush(colors[r.Next(0,5)]), p); //此处注意字体对象包括字体、像素、加粗与否等等
47 
48                 
49             }
50 
51             //绘制验证码上面的线
52             for (int i = 0; i < 50; i++)
53             {
54                 Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
55                 Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
56                 g.DrawLine(new Pen(Brushes.Green), p1, p2);
57             }
58 
59             //将验证码上面加一些小点点
60             for (int i = 0; i < 500; i++)
61             {
62                 Point p3 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
63                 bmp.SetPixel(p3.X,p3.Y,Color.Yellow);
64             }
65             //将画好的位图赋值给Image属性,即将画好的图片镶嵌到pictureBox中
66             pictureBox1.Image = bmp;
67         }
68     }
69 }
绘制数字验证码

实现效果图:

 

posted @ 2015-09-24 10:55  YouxiBug  阅读(388)  评论(0编辑  收藏  举报