cs:
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 GDI_绘图
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
string code;
Random r = new Random();
//加载事件
private void Form3_Load(object sender, EventArgs e)
{
setCode(4);//验证码四个长度
}
private void setCode(int length)
{
code = "";
for (int i = 0; i < length; i++)
{
int type = r.Next(0,2);//存在两次
if (type==0)
{
code += r.Next(0, 9);
}
else if (type==1)
{
code += (char)r.Next(97,123);
}
}
if (string.IsNullOrWhiteSpace(code))
{
return;
}
Bitmap img = new Bitmap(code.Length*20+10,30);
Graphics graphics = Graphics.FromImage(img);
graphics.Clear(Color.White);
Pen pen = new Pen(Color.Black,1);
graphics.DrawRectangle(pen,0,0,img.Width-1,img.Height-1);
for (int i = 0; i < code.Length; i++)
{
Pen p = new Pen(Color.FromArgb(r.Next(255),r.Next(255),r.Next(255)),r.Next(1,2));//画线
graphics.DrawLine(p,r.Next(0,img.Width),r.Next (0,img.Height),r.Next (img.Width),r.Next(0,img.Height));//线段的两个点
}
graphics.DrawString(code, new Font("宋体",15,FontStyle.Italic|FontStyle.Bold),new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255))),new Point(5,5));
for (int i = 0; i < code.Length*20; i++)
{
graphics.FillEllipse(new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255))),r.Next(0,img.Width),r.Next(0,img.Height),2,2);//绘制小圆点
}
pictureBox1.Image = img;
}
//确认
private void button1_Click(object sender, EventArgs e)
{
if (code.ToUpper().Equals(textBox1.Text.ToUpper()))
{
MessageBox.Show("正确");
}
else
{
MessageBox.Show("请重新输入");
setCode(4);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
setCode(4);
}
}
}
绘制字母数字圆点线条;利用picbox装起来;
来源:CSDN
作者:方丈山
链接:https://blog.csdn.net/qq_44820230/article/details/103757246