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"> ...
随机推荐
- vue中移动端滚动事件,点击一次触发了事件两次(better-scroll)
解决办法一: 将button标签换成a标签 问题代码: <span class="submitBtn" @click.stop="replyReport()&quo ...
- c#传入类名插入多条数据
public int Insert<T>(IReadOnlyCollection<T> models) where T : class, new() { int sucess ...
- 辨析 const指针 和 指向常量的指针
辨析以下几种指针p的定义. ; int *p = &tmp; const int *p = &tmp; int const* p = &tmp; int * const p = ...
- bom and dom
bom:Broswer Object Model: 浏览器对象模型- navigator: 获取客户机的信息(浏览器的信息)document.write(navigator.appName);- sc ...
- Linux系统组成和获取命令帮助3
命令的语法通用格式: # COMMAND OPTIONS ARGUMENTS COMMAND: 发起一个命令:请求内核将某个二进制程序运行为一个进程 ...
- UVa10474 Where is the Marble?(排序sort)
今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记alg ...
- iOS 获取手机型号(已更新至iPhone11)
+ (NSString *)iphoneType { // 需要导入头文件:#import <sys/utsname.h> struct utsn ...
- ABC007D Small Multiple[最短路]
题意:求$K$的倍数中数位和的最小值. 一开始有一种思路:由于产生答案的数字可能非常大,不便枚举,考虑转化为构造一个数字可以有$x\mod k=0$.然后二分答案数位和,数位DP检验是否存在,但是由于 ...
- python--AutoPy库
包括用于控制键盘和鼠标,在屏幕上查找颜色和位图以及显示警报的功能 - 所有这些都是以跨平台,高效和简单的方式进行的.适用于Mac OS X,Windows和X11 中文文档:https://blog. ...
- nodejs+mysql 批量更新
没办法,只能通过循环一次次更新: var updateMysql = function(){ data = excel[0].data; for(var i=1; i<data.length; ...