窗体中,需要自己增加的控件:
一个picturebox控件pbVerifyCode,
一个按钮butUpdateVerifyCode(看不清楚,换一张图);
其它代码可复制使用。
`public partial class FrmLogin : Form
{
//随机码的长度
private const int iVerifyCodeLength = 6;
//随机码
private String strVerifyCode = “”;
public FrmLogin()
{
InitializeComponent();
UpdateVerifyCode();
}
//更新验证码
private void UpdateVerifyCode()
{
strVerifyCode = CreateRandomCode(iVerifyCodeLength);
CreateImage(strVerifyCode);
}
private string CreateRandomCode(int iLength)
{
int rand;
char code;
string randomCode = String.Empty;
//生成一定长度的验证码
System.Random random = new Random();
for (int i = 0; i < iLength; i++)
{