2024年4月25日发(作者:)

//窗体登录事件产生验证码

//验证码的数值记录在number变量里面

//PictureBox1控件用来放验证码

using System;

using c;

using entModel;

using ;

using g;

using ;

using ;

using ;

using g2D;

using 实体类库_Model_;

using 业务逻辑层_BLL_;

namespace course5

{

public partial class loginform : Form

{

public static string number;

public loginform()

{

InitializeComponent();

}

//产生验证字符串、、、、

private string checkcode()

{

int num; // 保存产生的随机数

char code; // 保存随机数转换的字符

string check = "";//保存验证码

Random random = new Random();//随机数产生器

for (int i = 0; i < 4; i++)

{

num = ();

if (num % 2 == 0)

code = (char)('0' + (char)(num % 10));

else

code = (char)('A' + (char)(num % 26));

check += "" + ng();

}

return check;

}

private void codeimage(string checkcode)

{

if (checkcode == null || checkcode == "")

return;//产生位图 参数:长度高度

Bitmap image = new Bitmap((int)g( * 12.5), 21);

Graphics g = age(image);

try

{

Random random = new Random();

();//白色背景

for (int i = 0; i <=5; i++)

{//画背景的黑色线

ne(new Pen(), (),

(), (), ());

}

for (int i = 0; i <= 3; i++)

{//画背景的红色线

ne(new Pen(), (),

(), (), ());

}

Font font = new Font("宋体", 12);

//验证码写到图片上

for (int i = 0; i < ; i++)

{

SizeF f = eString(checkcode[i].ToString(), font);

//ateTransform(0.01f, 0);

Matrix m = orm;

(0.4f, 0.0f);

orm = m;

ring(checkcode[i].ToString(), font, new SolidBrush(),

( - 8) * i - 5, 2);

} for (int i = 0; i < 100; i++)

{// 画噪黑色音点

el((), (),

);

}

for (int i = 0; i < 80; i++)

{// 画噪红色音点

el((), (),

);

}

// 截成矩形

ctangle(new Pen(), 0, 0, - 1,

- 1);

= ;

= ;

oundImage = image;

}

catch

{

}

}

private void loginform_Load(object sender, EventArgs e)

{

//验证码

number = checkcode();

codeimage(number);

}

}

}