您的位置:首页 > 运维架构 > 网站架构

--网站注册验证码原理,完整实例(数字,字母,中文,混合),附原码--

2008-06-14 18:33 519 查看
--------------------------------------------------------

网站注册验证码原代码下载,下载地址:

http://files.cnblogs.com/enric1985/验证码.rar

愿对大家的学习有所帮助!

-------------------------------------------------------

众所周知, 我们在好多网站注册的时候,或者在发表一些评论的时候都会要求输入验证码!!!哎,说实话从用户体验的角度上来说,我TMD的很烦这个~~``

不过我们也不能光用,我们也来看看它是如何实现的!!

其实这个的关键就是生成的那个图象,我把核心代码贴出来,具体如下:

private void CreateImg(string strCode)

{

//创建Bmp位图

Bitmap bitMapImage = new Bitmap(Server.MapPath(strImgPath));

Graphics graphicImage = Graphics.FromImage(bitMapImage);

/**//*

也可以不用指定的图片,而是画一个背景图片

System.Drawing.Bitmap bitMapImage = new Bitmap(70, 30);

Graphics graphicImage = Graphics.FromImage(bitMapImage);

graphicImage.FillRectangle(new SolidBrush(Color.White), 0, 0, 70, 30);

*/

//设置画笔的输出模式

graphicImage.SmoothingMode = SmoothingMode.HighSpeed;

//添加文本字符串

graphicImage.DrawString(strCode, new Font("Arial", 20, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));

//画图片的前景噪音点

Random randomPixel = new Random();

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

{

int x = randomPixel.Next(bitMapImage.Width);

int y = randomPixel.Next(bitMapImage.Height);

bitMapImage.SetPixel(x, y, Color.FromArgb(randomPixel.Next(0, 255), randomPixel.Next(0, 255), randomPixel.Next(0, 255)));

}

//设置图像输出的格式

Response.ContentType = "image/jpeg";

//保存数据流

bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);

//释放占用的资源

graphicImage.Dispose();

bitMapImage.Dispose();

}

其中的要转入的参数 strCode, 就是你随机生成的数字啊..或者字母啊,或者中文字符啊...这部分代码就不贴出来了! 具体的实现都在实例的代码里头!大家看看其实很简单~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: