数字验证码
前言
C#实践开发_Winform 系列第七篇:随机数字验证码,掌握如何创建Graphics类,并用于图像处理。一、结果呈现
1. 界面设计
窗体界面设计:一个标签控件,一个文本框控件,一个Button按钮控件,一个pictureBox图像框控件。2. 运行结果呈现
二、源码
1.Form.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 test5
{
public partial class Form1 : Form
{
private string str_ValidateCode;
public Form1()
{
InitializeComponent();
}
public string GetRandomNumberString(int int_NumberLength)
{
string str_Number = string.Empty;
Random theRandomNumber = new Random();
for (int int_index = 0; int_index < int_NumberLength; int_index++)
str_Number += theRandomNumber.Next(10).ToString();
return str_Number;
}
public Color GetRandomColor() //生成随机颜色
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
//随机数
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Second = new Random((int)DateTime.Now.Ticks);
//为了便于在白色背景下显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Second.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue =