--网站注册验证码原理,完整实例(数字,字母,中文,混合),附原码--
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, 就是你随机生成的数字啊..或者字母啊,或者中文字符啊...这部分代码就不贴出来了! 具体的实现都在实例的代码里头!大家看看其实很简单~
网站注册验证码原代码下载,下载地址:
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, 就是你随机生成的数字啊..或者字母啊,或者中文字符啊...这部分代码就不贴出来了! 具体的实现都在实例的代码里头!大家看看其实很简单~
相关文章推荐
- java典型模块实例1:英文,数字,中文混合的验证码
- 原生js实现数字字母混合验证码的简单实例
- PHP制作数字字母混合验证码、图片验证码、中文验证码
- 原生js实现数字字母混合验证码的简单实例
- .NET下实现数字和字符相混合的验证码实例
- asp.net GDI+ 绘制验证码 大小写字母数字混合
- 如何生成汉字验证码或字母数字混合验证码
- 转载 支持中文字母数字、自定义字体php验证码程序
- C# 生成四位数字字母混合验证码
- 字母验证码 数字,字母数字混合也可
- 验证码的三种做法[纯数字][纯汉字][字母\数字\汉字混合]C#
- 支持中文字母数字、自定义字体php验证码代码
- 英文,数字和中文混合的彩色验证码实现
- 支持中文字母数字、自定义字体php验证码程序
- 绘制验证码----字母和数字混合的验证码
- php中文字母数字验证码实现代码
- 验证码的三种做法[纯数字][纯汉字][字母/数字/汉字混合]C#
- 使用 Python 生成类数字字母混合验证码图片
- 网站验证码的技术原理和实例解析
- 支持中文、字母、数字的PHP验证码