实现代码

 /// <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. SQL基础日期函数

    --dateadd 将制定的数值添加到指定的日期部分后的日期 select dateadd(mm,4,'01/01/99') -- 返回:以当前的日期格式返回05/01/99 --datediff 二 ...

  2. linux page allocation and deallocation

      All of the physical pages in the system are described by the mem_map  data structure which is a li ...

  3. C++对象模型——默认构造函数的合成

    最近在学习C++对象模型,看的书是侯捷老师的<深度探索C++对象模型>,发现自己以前对构造函数存在很多误解,作此笔记记录. 默认构造函数的误解 1.当程序猿定义了默认构造函数,编译器就会直 ...

  4. Oracle 从共享池删除指定SQL的执行计划

    ORACLE从共享池删除指定SQL的执行计划 2016-12-29 11:14 by 潇湘隐者, 2836 阅读, 0 评论, 收藏, 编辑 Oracle 11g在DBMS_SHARED_POOL包中 ...

  5. sring 监听器

    参考链接:http://blog.csdn.net/ysughw/article/details/8992322 sring容器在web程序中获取的方式:http://blog.csdn.net/aq ...

  6. DevExpress v18.1新版亮点——ASP.NET Bootstrap篇(二)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET Bootstrap v18.1 的新功能,快 ...

  7. MyEclipse移动开发教程:设置所需配置的iOS应用(一)

    MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] 一.iOS应用程序配置要求 这个进程需要四个需求数据文件: 证书签名请求(CSR)文件 证书签名请 ...

  8. 第一次做Java程序注意事项

    public class myapp{ public static void main(String[] args){ System.out.println("Hallo Java!&quo ...

  9. jquery日期和时间的插件精确到秒

    首先.html5提供了input的time类型,使我们可以通过input框输入日期,但是如果我们的需求是这个时间需要明确到几时几分几秒的,那html5就没有办法满足我们的需求了,就需要使用jQuery ...

  10. java的大数运算模板

    import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(S ...