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"> ...
随机推荐
- contextlib:上下文管理器工具
介绍 contextlib模块包含的工具可以用于处理上下文管理器和with语句 上下文管理器API ''' 上下文管理器(context manager)负责管理一个代码块中的资源,会在进入代码块时创 ...
- nginx的反向代理的优势,特点于原理(一)
说到反向代理,首先先说一下反向代理的概念 反向代理(Reverse Proxy)方式是指以代理服务器来接受客户端的连接请求,然后将请求转发给网络上的web服务器(可能是apache,nginx,tom ...
- MySQL 高级 视图 事物 触发器 函数 索引优化
视图 1.什么是视图 视图就是通过查询得到一张虚拟表,然后保存下来,下次直接使用即可 2.为什么要用视图 如果要频繁使用一张虚拟表,可以不用重复查询 3.如何用视图 create view t ...
- python基础编程:生成器、迭代器、time模块、序列化模块、反序列化模块、日志模块
目录: 生成器 迭代器 模块 time 序列化 反序列化 日志 一.生成器 列表生成式: a = [1,2,3,3,4,5,6,7,8,9,10] a = [i+1 for i in a ] prin ...
- IIS 调试配置
- PAT乙级1011
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805312417021952 题解 很明显这题是考数值范围的,i ...
- err:LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLE
err:LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLE err:LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLE err:LIBUSB_S ...
- win10日历交互效果
win10日历 早就想试着实现以下win10日历的动态css效果,现在终于有时间试试啦.本篇文章只是实现简单的效果,进阶篇后续会放上来 目标效果 鼠标移入目标元素,周围相关八块元素点亮,点亮高光范围呈 ...
- js 获取两个数组的交集,并集,补集,差集
https://blog.csdn.net/piaojiancong/article/details/98199541 ES5 const arr1 = [1,2,3,4,5], arr2 = [5, ...
- Logitech G系鼠标脚本编程,实现鼠标自动定位控制
利用罗技官方提供的API来写一个鼠标自动定位移动脚本 点击脚本编辑器中的帮助选项,查看罗技官方提供的API说明,有很多实现好的鼠标功能 G-series Lua API V8.45 Overview ...