代码语言
.
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/8
/
816
<div><span style="color: rgb(0,0,0)"></span><span style="color: rgb(0,0,255)">protected</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Page_Load(</span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> sender, EventArgs e) { CreateCheckCodeImage(GenerateCheckCode()); }</span></div> <div></div> <div></div> <div><span style="color: rgb(0,0,0)"><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> CreateCheckCodeImage(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> checkCode) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (checkCode </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">||</span><span style="color: rgb(0,0,0)"> checkCode.Trim() </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> String.Empty) </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)">; System.Drawing.Bitmap image </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.Bitmap((</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)">)Math.Ceiling((checkCode.Length </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">12.5</span><span style="color: rgb(0,0,0)">)), </span><span style="color: rgb(128,0,128)">22</span><span style="color: rgb(0,0,0)">); Graphics g </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Graphics.FromImage(image); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">生成随机生成器</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> Random random </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Random(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">清空图片背景色</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> g.Clear(Color.White); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">画图片的背景噪音线</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x1 </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(image.Width); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x2 </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(image.Width); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> y1 </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(image.Height); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> y2 </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(image.Height); g.DrawLine(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Pen(Color.Black), x1, y1, x2, y2); } Font font </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.Font(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Arial</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">12</span><span style="color: rgb(0,0,0)">, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.Drawing2D.LinearGradientBrush(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Rectangle(</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, image.Width, image.Height), Color.Blue, Color.DarkRed, </span><span style="color: rgb(128,0,128)">1.2f</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">); g.DrawString(checkCode, font, brush, </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">画图片的前景噪音点</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">100</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(image.Width); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> y </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">画图片的边框线</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> g.DrawRectangle(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Pen(Color.Silver), </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, image.Width </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">, image.Height </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">); System.IO.MemoryStream ms </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">image/Gif</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; Response.BinaryWrite(ms.ToArray()); } </span><span style="color: rgb(0,0,255)">finally</span><span style="color: rgb(0,0,0)"> { g.Dispose(); image.Dispose(); } }</span></span></div> <div></div> <div></div> <div></div> <div><span style="color: rgb(0,0,0)"><span style="color: rgb(0,0,0)"><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> GenerateCheckCode() { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> number; </span><span style="color: rgb(0,0,255)">char</span><span style="color: rgb(0,0,0)"> code; </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> checkCode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> String.Empty; Random random </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Random(); </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">4</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { number </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> random.Next(); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (number </span><span style="color: rgb(0,0,0)">%</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) code </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">char</span><span style="color: rgb(0,0,0)">)(</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">0</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">char</span><span style="color: rgb(0,0,0)">)(number </span><span style="color: rgb(0,0,0)">%</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">10</span><span style="color: rgb(0,0,0)">)); </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> code </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">char</span><span style="color: rgb(0,0,0)">)(</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">A</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">char</span><span style="color: rgb(0,0,0)">)(number </span><span style="color: rgb(0,0,0)">%</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">26</span><span style="color: rgb(0,0,0)">)); checkCode </span><span style="color: rgb(0,0,0)">+=</span><span style="color: rgb(0,0,0)"> code.ToString(); } Response.Cookies.Add(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> HttpCookie(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">CheckCode</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, checkCode)); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> checkCode; }</span></span></span></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