代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
验证码通用类
作者:
Dezai.CN
/ 发布于
2011/7/22
/
3154
<div> <div>using System;</div> <div>using System.Collections;</div> <div>using System.ComponentModel;</div> <div>using System.Data;</div> <div>using System.Drawing;</div> <div>using System.Web;</div> <div>using System.Web.SessionState;</div> <div>using System.Web.UI;</div> <div>using System.Web.UI.WebControls;</div> <div>using System.Web.UI.HtmlControls;</div> <div>namespace WebApplication1.验证码</div> <div>{</div> <div>/// <summary></div> <div>/// 完美随机验证码 0.10</div> <div>/// Verion:0.10</div> <div>/// Description:随机生成设定验证码,并随机旋转一定角度,字体颜色不同</div> <div>/// </summary></div> <div>public class ValidateCode : System.Web.UI.Page</div> <div>{</div> <div>private void Page_Load(object sender, System.EventArgs e)</div> <div>{</div> <div>string randomcode = this.CreateRandomCode(4);</div> <div>Session["ValidateCode"] = randomcode;</div> <div>//ViewState["ValidateCode"] = randomcode;</div> <div>this.CreateImage(randomcode);</div> <div>} <div>/// <summary></div> <div>/// 生成随机码</div> <div>/// </summary></div> <div>/// <param name="length">随机码个数</param></div> <div>/// <returns></returns></div> <div>private string CreateRandomCode(int length)</div> <div>{</div> <div>int rand;</div> <div>char code;</div> <div>string randomcode = String.Empty;</div> <div>//生成一定长度的验证码</div> <div>System.Random random = new Random();</div> <div>for(int i=0;i<length;i++)</div> <div>{</div> <div>rand = random.Next();</div> if(rand%3 == 0)</div> <div>{</div> <div>code = (char)('A' + (char)(rand%26));</div> <div>} <div>else</div> <div>{</div> <div>code = (char)('0' + (char)(rand%10));</div> <div>} <div>randomcode += code.ToString();</div> <div>} <div>return randomcode;</div> <div>} <div>/// <summary></div> <div>/// 创建随机码图片</div> <div>/// </summary></div> <div>/// <param name="randomcode">随机码</param></div> <div>private void CreateImage(string randomcode)</div> <div>{</div> <div>int randAngle = 45; //随机转动角度</div> <div>int mapwidth = (int)(randomcode.Length * 23);</div> <div>Bitmap map = new Bitmap(mapwidth,28);//创建图片背景</div> <div>Graphics graph = Graphics.FromImage(map);</div> <div>graph.Clear(Color.AliceBlue);//清除画面,填充背景</div> <div>graph.DrawRectangle(new Pen(Color.Black,0),0,0,map.Width-1,map.Height-1);//画一个边框</div> <div>//graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式</div> <div>Random rand = new Random();</div> <div>//背景噪点生成</div> <div>Pen blackPen = new Pen(Color.LightGray,0);</div> <div>for (int i=0;i<50;i++)</div> <div>{</div> <div>int x = rand.Next(0,map.Width);</div> <div>int y = rand.Next(0,map.Height);</div> <div>graph.DrawRectangle(blackPen,x,y,1,1);</div> <div>} <div>//验证码旋转,防止机器识别</div> <div>char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组</div> <div>//文字距中</div> <div>StringFormat format = new StringFormat(StringFormatFlags.NoClip);</div> <div>format.Alignment = StringAlignment.Center;</div> <div>format.LineAlignment = StringAlignment.Center;</div> <div>//定义颜色</div> <div>Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };</div> <div>//定义字体</div> <div>string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };</div> <div>for(int i=0;i<chars.Length;i++)</div> <div>{</div> <div>int cindex = rand.Next(7);</div> <div>int findex = rand.Next(5);</div> <div>Font f = new System.Drawing.Font(font[findex],13,System.Drawing.FontStyle.Bold);//字体样式(参数2为字体大小)</div> <div>Brush b = new System.Drawing.SolidBrush(c[cindex]);</div> <div>Point dot = new Point(16,16);</div> <div>//graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150);//测试X坐标显示间距的</div> <div>float angle = rand.Next(-randAngle,randAngle);//转动的度数</div> <div>graph.TranslateTransform(dot.X,dot.Y);//移动光标到指定位置</div> <div>graph.RotateTransform(angle);</div> <div>graph.DrawString(chars[i].ToString(),f,b,1,1,format);</div> <div>//graph.DrawString(chars[i].ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format);</div> <div>graph.RotateTransform(-angle);//转回去</div> <div>graph.TranslateTransform(2,-dot.Y);//移动光标到指定位置</div> <div>} <div>//graph.DrawString(randomcode,fontstyle,new SolidBrush(Color.Blue),2,2); //标准随机码</div> <div>//生成图片</div> <div>System.IO.MemoryStream ms = new System.IO.MemoryStream();</div> <div>map.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);</div> <div>Response.ClearContent();</div> <div>Response.ContentType = "image/gif";</div> <div>Response.BinaryWrite(ms.ToArray());</div> <div>graph.Dispose();</div> <div>map.Dispose();</div> <div>} <div>region Web 窗体设计器生成的代码</div> <div>override protected void OnInit(EventArgs e)</div> <div>{</div> <div>//</div> <div>// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。</div> <div>//</div> <div>InitializeComponent();</div> <div>base.OnInit(e);</div> <div>} <div>/// <summary></div> <div>/// 设计器支持所需的方法 - 不要使用代码编辑器修改</div> <div>/// 此方法的内容。</div> <div>/// </summary></div> <div>private void InitializeComponent()</div> <div>{</div> <div>this.Load += new System.EventHandler(this.Page_Load);</div> <div>} <div>endregion</div> <div>} <div>} </div> <div></div>
试试其它关键字
验证码
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3