额,我用Winform做的,分享下- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace 验证码2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- Bitmap yanzheng=new Bitmap(200,70);//声明一个宽200,高70的位图
- Graphics g = Graphics.FromImage(yanzheng);//新建个绘图的对象
- Random n=new Random();//随机5个数
- string str = null;
- string[] font = { "黑体", "华文行楷", "微软雅黑", "隶书", "幼圆", "宋体", "华文彩文" };
- Color[] col = { Color.Green, Color.Blue, Color.Red, Color.Black, Color.BlueViolet, Color.GreenYellow, Color.Orange };
- for (int i = 0; i < 5; i++)//5个随机的数字
- {
- str+=n.Next(0,10).ToString();
- }
- //位图,画线,画点,随机字体,随机颜色
- for (int i = 0; i < 100; i++)//噪线
- {
- Point p1=new Point(n.Next(0,yanzheng.Width),n.Next(0,yanzheng.Height));//两点一线,这是第一个点的随机坐标
- Point p2=new Point(n.Next(0,yanzheng.Width),n.Next(0,yanzheng.Height));//这是第二个点的随机坐标,都控制在位图的宽和高之内
- Pen pen=new Pen(col[n.Next(0,7)],5);
- g.DrawLine(pen,p1,p2);
- }
- for (int i = 0; i < 200; i++)//噪点
- {
- Point p=new Point(n.Next(0,yanzheng.Width),n.Next(0,yanzheng.Height));
- yanzheng.SetPixel(p.X, p.Y, col[n.Next(0, 7)]);
- }
- for (int i = 0; i < 5; i++)
- {
- Point p=new Point(i*30,n.Next(0,6)*5);//遍历的坐标,每一个坐标的宽度会加30,高度随机
- g.DrawString(str[i].ToString(),new Font(font[n.Next(0,7)],40),new SolidBrush(col[n.Next(0,7)]),p);//把文本写进位图内,随机颜色,随机字体
- }
- pictureBox1.Image = yanzheng;//最后把这个位图赋值给图片
- }
- }
- }
复制代码
附上图,点击图片更换验证码 |
|