生成验证码的部分代码(11)

本文介绍了一种生成随机验证码并将其转化为图片的方法,通过使用C#编程语言,实现了数字与字母混合的验证码生成,同时加入了干扰线和前景点以增强安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


开发工具与关键技术: 生成验证码的部分代码(11)

作者:冉启东  年级:1804

撰写时间:2019年6月5日                

本技术文献起止时间 2019年 5月28日~ 2019年6月8日

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

随机验证码生成的部分代码

publicstaticclassValidCodeUtils

//公共静态

{

 ///<summary>

///获得随机字符串

///</summary>

///<paramname="intLength">随机数的长度</param>

 ///<returns>随机数字符串</returns>

publicstaticstring GetRandomCode(int intLength)

{ /*产生数字和密码混合的随机数*/

string strReturn = string.Empty;  Random random = newRandom();

//随机数for (int i = 0; i < intLength; i++){ char cRerult; int intRandom = random.Next();

//产生一个非负随机整数

/*根据当前随机数来确定字符串*/

//intRandom% 3 获取的是intRandom/

3 得到的余数if (intRandom % 3 == 0)

{

//产生数字//位数来产生数字cRerult = (char)(0x30 + (intRandom% 10));

}

elseif

(intRandom% 3 == 1)

{

//位数产生大写字母:大写字符 65-97 A 65

//68D  25 ZcRerult = (char)(0x41 + (intRandom % 0x1a));

}

 else {

 //余数为2

//产生小写字母 98-116

cRerult= (char)(0x61 + (intRandom% 0x1a)); }strReturn += cRerult.ToString()

}

return strReturn;

}

///<summary>

///根据字符串创建验证码

///</summary>

///<paramname="strRandom">字符串</param>

///<returns>图片</returns>

publicstaticbyte[] CreateImage(string strRandom){

 //新增图片Bitmap newBitmap = new

Bitmap(strRandom.Length *20, 38);

 Graphics g = Graphics.FromImage(newBitmap);

 g.Clear(Color.White);

//在图片上绘制文字SolidBrush solidBrush = newSolidBrush(Color.Red);

g.DrawString(strRandom,

newFont("Aril", 17), solidBrush,12, 1);

 //在图片上绘制干扰线Random random = newRandom();

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

{//产生一条线,并绘制到画布。 起始点(x,y)  总结点

int x1 =random.Next(newBitmap.Width);

int y1 = random.Next(newBitmap.Height);

 int x2 = random.Next(newBitmap.Width);

int y2 =random.Next(newBitmap.Height);

 g.DrawLine(newPen(Color.DarkGray), x1, y1, x2, y2);

 }

//绘制图片的前景干扰点

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

int x =random.Next(newBitmap.Width);

int y = random.Next(newBitmap.Height);

newBitmap.SetPixel(x,y, Color.FromArgb(random.Next()));}

//在最外边绘制边框g.DrawRectangle

(newPen(Color.Blue), 0, 0,

newBitmap.Width,newBitmap.Height);

 g.DrawRectangle(newPen(Color.Blue),

-1,-1, newBitmap.Width, newBitmap.Height);

//将图转保存到内存流中MemoryStream ms = newMemoryStream();newBitmap.Save(ms, ImageFormat.Jpeg); return ms.ToArray();//将流内容写入byte数组返回}}}

效果图如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/20190610080931401.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU0MDY3Mg==,size_16,color_FFFFFF,t_70)
刷新一次变换一次![在这里插入图片描述](https://img-blog.csdnimg.cn/20190610080940572.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU0MDY3Mg==,size_16,color_FFFFFF,t_70)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值