C#产生随机验证码的代码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Text;using System.Drawing.Drawing2D;using System.Drawing;using System.IO;using System.Drawing.Imaging;public partial class ValidateCode : System.Web.UI.Page{ private string code; private const int ImageHeigth = 22; //验证码图片的高度 private const double ImageLengthBase = 12.5; //验证码图片中每个字符的宽度 private const int ImageLineNumber = 25; //噪音线的数量 private const int ImagePointNumber = 100; //噪点的数量 private int length; public static string VALIDATECODEKEY; //此处用来保存验证码到Session的Key的名称 //静态构造函数初始化验证码在Session中的Key名称 static ValidateCode() { VALIDATECODEKEY = "VALIDATECODEKEY"; } //设置验证码的长度和内容 public ValidateCode() { this.length = 4; this.code = string.Empty; } /// /// 产生随机的验证码并加入Session /// /// 验证码长度 /// public string CreateCode(int length) { if (length <= 0) { return string.Empty; } Random random = new Random(); StringBuilder builder = new StringBuilder(); //产生随机的验证码并拼接起来 for (int i = 0; i < length; i++) { builder.Append(random.Next(0, 10)); } this.code = builder.ToString(); this.Session[VALIDATECODEKEY] = this.code; return this.code; } /// /// 根据长度产生验证码 /// 并将验证码画成图片 /// /// 验证码长度 public void CreateValidateImage(int length) { this.code = this.CreateCode(length); this.CreateValidateImage(this.code); } /// /// 根据产生的验证码生成图片 /// /// 验证码 public void CreateValidateImage(string code) { if (!string.IsNullOrEmpty(code)) { this.Session[VALIDATECODEKEY] = code; //初始化位图Bitmap对象,指定图片对象的大小(宽,高) Bitmap image = new Bitmap((int)Math.Ceiling((double)(code.Length * ImageLengthBase)), ImageHeigth); //初始化一块画布 Graphics graphics = Graphics.FromImage(image); Random random = new Random(); try { int num5; graphics.Clear(Color.White); //绘制噪音线 for (num5 = 0; num5 < ImageLineNumber; num5++) { int num = random.Next(image.Width); int num3 = random.Next(image.Height); int num2 = random.Next(image.Width); int num4 = random.Next(image.Height); graphics.DrawLine(new Pen(Color.Silver), num, num3, num2, num4); } //验证码字体样式 Font font = new Font("Tahoma", 12, FontStyle.Italic | FontStyle.Bold); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); //绘制验证码到画布 graphics.DrawString(code, font, brush, (float)2, (float)2); //绘制噪点 for (num5 = 0; num5 < ImagePointNumber; num5++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); MemoryStream stream = new MemoryStream(); //保存图片 image.Save(stream, ImageFormat.Gif); base.Response.ClearContent(); base.Response.ContentType = "image/Gif"; base.Response.BinaryWrite(stream.ToArray()); } finally { graphics.Dispose(); image.Dispose(); } } } protected override void OnLoad(EventArgs e) { this.CreateValidateImage(this.length); } // Properties public string Code { get { return this.Code; } } public int Length { get { return this.length; } set { this.length = value; } }} |
再次申明非本人原创。可以到网上下载ASPNETAJAXWeb.ValidateCode.dll直接使用
C#产生随机验证码的代码的更多相关文章
- Python生成随机验证码,大乐透号码
实例笔记之生成随机号码 扩展知识 - yield(生成器) 随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__' ...
- 【代码实现】PHP生成各种随机验证码
原文地址:http://www.phpthinking.com/archives/531 验证码在WEB应用中很重要,通经常使用来防止用户恶意提交表单,如恶意注冊和登录.论坛恶意灌水等.本文将通过实例 ...
- 随机验证码生成(python实现)
需求:生成随机不重复验证码. 代码: #!/usr/bin/env python # encoding: utf-8 """ @author: 侠之大者kamil @fi ...
- php学习笔记:利用gd库生成图片,并实现随机验证码
说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...
- python_way,day4 内置函数(callable,chr,随机验证码,ord),装饰器
python_way,day4 1.内置函数 - 下 制作一个随机验证码 2.装饰器 1.内置函数 - 下 callable() #对象能否被调用 chr() #10进制数字对应的ascii码表中的内 ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- python之使用PIL模块制作随机验证码
制作随机验证码,需要如下知识点: 1.随机验证码的制作(这里用的是random模块随机产生字符) 2.图片的制作 3.随机直线干扰项 4.其他随机干扰项 代码如下: from PIL import I ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
随机推荐
- fastadmin 页面添加编辑日期时间
添加 <div class="form-group"> <label class="control-label col-xs-12 col-sm-2&q ...
- How to: Compile Linux kernel 2.6
Compiling custom kernel has its own advantages and disadvantages. However, new Linux user / admin ...
- 自动化测试 selenium 测试软件安装
一.自动化测试优点 1.对程序的回归测试更方便.在程修改的比较平凡的时候,表现的更明显. 2.可以代替测试人员运行更繁琐的测试,也可以代替测试人员不可能完成的操作(比如连续点击50次) 3.更好的 ...
- MyBatis中<![CDATA[ ]]>的使用
原文地址:https://www.cnblogs.com/catgatp/p/6403382.html <![CDATA[]]>和转义字符 被<![CDATA[]]>这个标记所 ...
- Tomcat启动中文乱码,淇℃伅等
打开Tomcat根目录下的conf文件夹,打开logging.properties. 把 java.util.logging.ConsoleHandler.encoding = UTF-8 换成 ja ...
- 【Maven】-maven打包跳过javadoc
有时候由于代码中注释错误(比如方法参数)或者maven javadoc插件版本有问题,导致打包报错,而我们着急打包验证问题,没有时间一一修改错误,这时候可以先跳过生成javadoc,继续下一步工作. ...
- The Cost of JavaScript --------引用
tl;dr: 想要保持页面的快速运行,你需要仅加载当前页面所需的 JavaScript 代码.优先考虑用户所需,之后运用代码分离懒加载其他内容. Is it happening - 在这个时期,你可以 ...
- 什么是favicon.ico?
㈠定义 ⑴所谓favicon,即Favorites Icon的缩写,顾名思义,便是其可以让浏览器的收藏夹中除显示相应的标题外,还以图标的方式区别不同的网站. ⑵根据浏览器的不同,Favicon显示也有 ...
- PHP回顾(面向对象)
类中的成员属性不能够用函数为其赋值.public age = rand(1,100);//这是错误的: __get() __set() __isset() __unset() final 用来修 ...
- php+提高大文件上传速度
PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...