代码语言
.
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
】
ASP.NET 验证码
作者:
Dezai.CN
/ 发布于
2011/7/7
/
715
<div> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 随机数生成器,使用静态变量</font></td> </tr> </tbody> </table> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">private</font></strong> "csharp keyword"><strong><font color="#006699">static</font></strong> "csharp plain">Random rnd = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">Random();</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">protected</font></strong> "csharp keyword"><strong><font color="#006699">void</font></strong> "csharp plain">Page_Load("csharp keyword"><strong><font color="#006699">object</font></strong> "csharp plain">sender, EventArgs e)</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">{</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 验证码图片大小</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">width = 60;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">height = 20;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 位图</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">Bitmap img = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">Bitmap(width, height);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 画板</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">Graphics g = Graphics.FromImage(img);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 单色画笔(画背景色,字符串)</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">Brush b;</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 初始化为背景色(红绿蓝都在200到250之间)</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">b = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">SolidBrush(Color.FromArgb(rnd.Next(200, 250), rnd.Next(200, 250), rnd.Next(200, 250)));</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 画背景</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">g.FillRectangle(b, 0, 0, width, height);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 边框</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// g.FillRectangle(b, 0, 0, width - 1, height - 1); </font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 画笔(画直线或曲线)(红绿蓝都在160到200之间)</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">Pen p = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">Pen(Color.FromArgb(rnd.Next(160, 200), rnd.Next(160, 200), rnd.Next(160, 200)));</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 画干扰线</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">for</font></strong> "csharp plain">("csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">i = 0; i < 100; i++)</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">{</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">x1 = rnd.Next(width);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">y1 = rnd.Next(height);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 线条长度在 12 以内</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">x2 = x1 + rnd.Next(12);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">y2 = y1 + rnd.Next(12);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 绘制两个坐标之间的连线</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 参数:起点(x1, y1),终点(x2, y2);</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">g.DrawLine(p, x1, y1, x2, y2);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">}</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 验证码字体</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">Font f = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">Font("csharp string"><font color="#0000ff">"Courier New"</font>"csharp plain">, 16, FontStyle.Bold);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 验证码位置(左上角坐标)</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">PointF pf;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 验证码</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">StringBuilder sb = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">StringBuilder();</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 4 位验证码</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">for</font></strong> "csharp plain">("csharp keyword"><strong><font color="#006699">int</font></strong> "csharp plain">i = 0; i < 4; i++)</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">{</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 数字验证码</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp keyword"><strong><font color="#006699">string</font></strong> "csharp plain">s = rnd.Next(10).ToString();</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">sb.Append(s);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 验证码颜色(红绿蓝都在20到130之间)</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">b = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">SolidBrush(Color.FromArgb(rnd.Next(20, 130), rnd.Next(20, 130), rnd.Next(20, 130)));</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 可设置字符在图片中的位置</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">pf = "csharp keyword"><strong><font color="#006699">new</font></strong> "csharp plain">PointF(13 * i, -1);</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">g.DrawString(s, f, b, pf);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">}</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 保存验证码</font></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">Session["csharp string"><font color="#0000ff">"Captcha"</font>"csharp plain">] = sb.ToString();</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 将图片以流输出</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">Response.ContentType = "csharp string"><font color="#0000ff">"image/pjpeg"</font>"csharp plain">;</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">img.Save(Response.OutputStream, ImageFormat.Jpeg);</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content"></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp comments"><font color="#008200">// 释放资源</font></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">b.Dispose();</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">g.Dispose();</td> </tr> </tbody> </table> </div> <div class="line alt2"> <table> <tbody> <tr> <td class="content">"csharp plain">img.Dispose();</td> </tr> </tbody> </table> </div> <div class="line alt1"> <table> <tbody> <tr> <td class="content">"csharp plain">}</td> </tr> </tbody> </table> </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