实现代码

 /// <summary>
/// 生成验证码图片,保存session名称VerificationCode
/// </summary>
public static void CreateVerificationCode()
{
int number;
string checkCode = string.Empty; //随机数种子
Random randoms = new Random(); for (int i = ; i < ; i++) //校验码长度为4
{
//随机的整数
number = randoms.Next(); //字符从0-9,A-Z中随机产生,对应的ASCII码分别为
//48-57,65-90
number = number % ;
if (number < )
{
number += ;
}
else
{
number += ;
}
checkCode += ((char)number).ToString();
} //在session中保存校验码
System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode; //若校验码为空,则直接返回
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
//根据校验码的长度确定输出图片的长度
System.Drawing.Bitmap image = new System.Drawing.Bitmap(, );//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
//创建Graphics对象
Graphics g = Graphics.FromImage(image);
try
{
//生成随机数种子
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线 10条
//---------------------------------------------------
for (int i = ; i < ; i++)
{
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height); //用银色画出噪音线
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
//---------------------------------------------------
//Brush b = Brushes.Silver;
//g.FillRectangle(b, 0, 0, image.Width, image.Height);
//---------------------以上两种任选其一------------------------------
//输出图片中校验码的字体: 12号Arial,粗斜体
Font font = new Font("Arial", , (FontStyle.Bold | FontStyle.Italic)); //线性渐变画刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
g.DrawString(checkCode, font, brush, , ); //画图片的前景噪音点 50个
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
} //画图片的边框线
g.DrawRectangle(new Pen(Color.Peru), , , image.Width - , image.Height - ); //创建内存流用于输出图片
using (MemoryStream ms = new MemoryStream())
{
//图片格式指定为png
image.Save(ms, ImageFormat.Jpeg);
//清除缓冲区流中的所有输出
System.Web.HttpContext.Current.Response.ClearContent();
//输出流的HTTP MIME类型设置为"image/Png"
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
//输出图片的二进制流
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
}
finally
{
//释放Bitmap对象和Graphics对象
g.Dispose();
image.Dispose();
}
}

Create Verification Code

创建一个aspx页面

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %>

 <%Help.CreateVerificationCode(); %>

Create WebFrom

添加HTML代码,引用

 <div class="positionR">
<label>验证码:</label>
<span class="style1"> *</span>
<input type="text" class="yanZm" runat="Server" reg="^.+$" id="txtAuthCode" tip="请输入验证码!" />
<img class="yanZm_img" src="AuthCode.aspx" alt="" id="imgAuthCode" />
</div>

HTML Code

如何实现刷新?

     <script type="text/javascript">
$("#imgAuthCode").click(function () {
$(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime());
});
</script>

Refresh - JS Code

效果图

 注明:内容来源不可考 = = 验证码图片生成非原创,如果谁知道原作者请告诉我.

DEMO

ASP.NET 实现验证码以及刷新验证码的更多相关文章

  1. ASP.NET MVC3实现无刷新验证码

    在MVC中进行留言,评论等功能时,不可避免会用到表单提交时的验证码问题,有时,我们的作法是,当表单被提交后,在controller里去判断验证码的正确与否,但我认为这种用户体验是很差的,今天正好有后时 ...

  2. yourphp点击刷新验证码

    加入css <script type="text/javascript" src="./Public/Js/my.js"></script&g ...

  3. J2EE如何生成验证码图片和点击刷新验证码

    验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...

  4. ASP.NET 如何做出简单的验证码

    如果说要做验证码,那不得不提的就是GDI+绘图了.我们都知道验证码是以图片形式展示的,而且是动态生成的,这样就需要我们去画出它. 科普一下,什么是GDI+? GDI+是图形设备接口(GDI)的高级版本 ...

  5. 解决Yii2中刷新网页时验证码不刷新的问题

    解决Yii2中刷新网页时验证码不刷新的问题 [ 2.0 版本 ] ljfrocky  2015-05-30 19:39:00  1304次浏览 5条评论 10110 在Yii2框架中,如果在表单中使用 ...

  6. C#之asp.net 及MVC 生成动态验证码:

    C#之asp.net 及MVC 生成动态验证码: 1.生成验证码字符串 // 随机生成指定长度的验证码字符串private string RandomCode(int length) { string ...

  7. 用户登录ajax局部刷新验证码

    用户登录的时候,登录页面附带验证码图片,用户需要输入正确的验证码才可以登录,验证码实现局部刷新操作. 效果如图: 代码如下: #生成验证码及图片的函数  newcode.py import rando ...

  8. S2SH框架中的无刷新验证码功能实现

    暑假期间在实验室做使用S2SH框架的项目,其中登录和注册需要验证码,实现了一个没有实现刷新验证码功能的简单版本,代码如下: 1 package com.sem.action; 2 3 import j ...

  9. [oldboy-django][2深入django]点击刷新验证码

    # 点击更新验证码,只要重新在发送一个请求即可 <img src="/check_code/" onclick="updateCode(this);" w ...

随机推荐

  1. jsp jsp常用指令

    jsp指令是为jsp引擎设计的,他们并不直接产生任何可见输出,而只是告诉引擎如何处理jsp页面中的其余部分. jsp中的指令 page指令 include指令 taglib指令 jsp指令的基本语法 ...

  2. sgu 125 Shtirlits dfs 难度:0

    125. Shtirlits time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a checkered fi ...

  3. artDialog 弹窗提示

    artDialog 弹窗提示,方便调用,不用去查文档了. /// <reference path="../../Scripts/artDialog5.0/artDialog.min.j ...

  4. delphi中TQueue的使用问题

    TQueue里存放的是指针,所要存储的内容最好建立在堆上,在pop方法之后释放掉这个空间. 实例代码: MMSQueue:= TQueue.Create; MMSQueue.Push(StrNew(P ...

  5. focusSNS学习笔记

    FocusSNS是一个社交类型的网站架构 系统的加载过程 所有的分发都从RouteController开始 @RequestMapping(value={"/", "/h ...

  6. tomcat的简单配置与适用默认的web应用

    指定tomcat端口: server.xml: <Connector port="8080" protocol="HTTP/1.1" connection ...

  7. Linux便捷命令

    快捷键: tab:命令和文件名称补齐功能快捷键 Ctrl + c:中断当前程序 Ctrl + d:退出当前终端 求助快捷键: man:manual的简写,如man ls man命令: / string ...

  8. L209

    China's Chang'e-4 probe entered a planned orbit Sunday morning // to prepare for the first-ever soft ...

  9. js中定时器

    周期性定时器:周期性的执行某段代码 window.setInterval()      window.clearInterval() 示例: document.it = setInterval(fun ...

  10. iOS项目实现SVN代码管理方法③(Part 三)

    内容中包含 base64string 图片造成字符过多,拒绝显示