摘自:http://blog.csdn.net/durongjian/article/details/4336380

一、创建ValidaeCode类库工程

1、创建ValidaeCode类库工程,在[解决胜方案资源管理器]面板中,右键单击[ValidateCode]节点,并选择[属性]命令。

2、单击[属性]命令,选中[应用程序],半分别把程序集名称和默认命名空间两个属性值设为“ASPNETAJAXWeb.ValidateCode”和“ASPNETAJAXWeb.ValidateCode.Page”。

3、ValidateCode.cs:

using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
/// <summary>
///ValidateCode 的摘要说明
/// </summary>
namespace ASPNETAJAXWeb.ValidateCode.Page
{
public class ValidateCode : System.Web.UI.Page
{
private const double IMAGELENGTHBASE = 12.5;
private const int IMAGEHEIGTH = ;
private const int IMAGELINENUMBER = ;
private const int IMAGEPOINTNUMBER = ;
public static string VALIDATECODEKEY = "VALIDATECODEKEY";
private int length = ;//验证码的数字个数
private string code = string.Empty;
public ValidateCode()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public int Length
{
get { return length; }
set { length = value; }
}
public string Code
{
get { return Code; }
} /// <summary>
/// 创建随机验证码
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public string CreateCode(int length)
{
if (length <= )
return string.Empty;
Random random = new Random();
StringBuilder sbCode = new StringBuilder();
for (int i = ; i < length; i++)
{
sbCode.Append(random.Next(, ));
}
code = sbCode.ToString();
Session[VALIDATECODEKEY] = code;
return code;
} /// <summary>
/// 创建验证码的图片和验证码
/// </summary>
/// <param name="code"></param>
public void CreateValidateImage(string code)
{
if (string.IsNullOrEmpty(code) == true)
return;
Session[VALIDATECODEKEY] = code;
Bitmap image = new Bitmap((int)Math.Ceiling((code.Length * IMAGELENGTHBASE)), IMAGEHEIGTH);
Graphics g = Graphics.FromImage(image); ///随机数生成器
Random random = new Random();
try
{
g.Clear(Color.White);
///绘制图片的干扰线
int x1, x2, y1, y2;
for (int i = ; i < IMAGELINENUMBER; i++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Height);
y1 = random.Next(image.Width);
y2 = random.Next(image.Height);
} ///绘制验证码
Font font = new Font("TaHoma", , FontStyle.Bold | FontStyle.Italic);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(code, font, brush, 2.0f, 2.0f); ///画图片的前景噪音点
int x, y;
for (int i = ; i < IMAGEPOINTNUMBER; i++)
{
x = random.Next(image.Width);
y = random.Next(image.Height);
///绘制点
image.SetPixel(x, y, Color.FromArgb(random.Next()));
} ///
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - ); ///
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif); ///
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} protected override void OnLoad(EventArgs e)
{
CreateValidateImage(length);
}
public void CreateValidateImage(int length)
{
code = CreateCode(length);
CreateValidateImage(code);
}
}
}

   4、在另外一个ASP.NET应用程序中添加Web窗体页ValidateCode.aspx,并删除该页面的代码隐藏文件(ValidateCode.aspx.cs)

5、删除 ValidateCode.aspx页面的HTML代码,仅仅保留一行使用“@Page”指令的如下代码:

<%@ Page Language="C#" AutoEventWireup="false" Inherits="ASPNETAJAXWeb.ValidateCode.Page.ValidateCode" %>

6、使用图文验证页面:把 ValidateCode.aspx页面复制到要使用该页面的ASP.NET应用程序中,使用引用的方法把程序集添加到Bin目录下,在使用验证码的页面上添加一下Image控件,并将该控件的ImageUrl属性的值设置为ValidateCode.aspx。

<asp:Image ID="Image1" runat="server" ImageUrl="~/ValidateCode.aspx" />

7、可通过判断Session["VALIDATECODEKEY"]与用户所输入的值是否相等检查用户输入的验证码是否正确。

.net完整的图文验证的更多相关文章

  1. jquery-4 完整表单验证实例

    jquery-4 完整表单验证实例 一.总结 一句话总结:在form的jquery对象中返回false即可终止表单提交. 1.验证的显示错误消息如何布局? 开始时隐藏,出现错误后显示 10 .erro ...

  2. Linux系统(X64)安装Oracle11g完整安装图文教程另附基本操作

    一:查看本地ssh服务 Linux系统下安装启动ssh服务,下面以CentOS版本Linux系统为例: 1.检查是否装了SSH包 rpm -qa |grep ssh 2.没有安装SSH直接YUM安装 ...

  3. 完整注册+JQuery验证+selert后台校验

    Java代码 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  4. Android 代码混淆、第三方平台加固加密、渠道分发 完整教程(图文)

    第一步:代码混淆(注意引入的第三方jar) 在新版本的ADT创建项目时,混码的文件不再是proguard.cfg,而是project.properties和proguard-project.txt. ...

  5. Linux系统(X64)7 安装Oracle11g完整安装图文教程另附基本操作

    在linux 7.6 安装 oracle 11g    mount 挂载yum源 yum –y sys*  gcc*  lib* sys* ma* un* gli* elf* bin* com*   ...

  6. 比较完整的URL验证

    转自:http://wuchaorang.2008.blog.163.com/blog/static/4889185220135279223253/ function IsURL(str_url){v ...

  7. Linux系统(X32)安装Oracle11g完整安装图文教程另附基本操作

    一.修改操作系统核心参数 在Root用户下执行以下步骤: )修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/limi ...

  8. Linux 安装Oracle11g完整安装图文教程另附基本操作 (分享)

    一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...

  9. Asp.net MVC验证那些事(4)-- 自定义验证特性

    在项目的实际使用中,MVC默认提供的Validation Attribute往往不够用,难以应付现实中复杂多变的验证需求.比如, 在注册用户的过程中,往往需要用户勾选”免责声明”,这个checkbox ...

随机推荐

  1. RecyclerView 缓存机制详解

    一 前言 RecyclerView据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView.GridView.RecyclerView可以用来代替传 ...

  2. 阿里云96页报告详解《云上转型》(10个案例、10大趋势/完整版PPT)

    阿里云96页报告详解<云上转型>(10个案例.10大趋势/完整版PPT) 2017-12-29 14:20阿里云/云计算/技术 ﹃产业前沿超级干货﹄ ﹃数据观○重磅速递﹄ 阿里云研究中心云 ...

  3. Java多媒体编程应用

    1. 声音文件的播放 1.1 在Applet中播放声音 在Applet中,可以使用AudioClip来播放声音,它非常简单,只有三个方法:play().loop()和stop(). 例1.1 利用Au ...

  4. @ArrayList剖析

    第1部分 ArrayList介绍 ArrayList简介 Resizable-array implementation of the List interface. Implements all op ...

  5. iOS开发-CoreMotion框架(加速计和陀螺仪)

    CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...

  6. Ionic 4 and the Lifecycle Hooks

    原文: https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864 ------------------- ...

  7. Java8新特性之重复注解(repeating annotations)浅析

    Java8新特性之重复注解(repeating annotations)浅析 学习了:https://www.jb51.net/article/50827.htm

  8. Ensemble_learning 集成学习算法 stacking 算法

    原文:https://herbertmj.wikispaces.com/stacking%E7%AE%97%E6%B3%95 stacked 产生方法是一种截然不同的组合多个模型的方法,它讲的是组合学 ...

  9. [Javascript Crocks] Make your own functions safer by lifting them into a Maybe context

    You probably have functions that aren’t equipped to accept a Maybe as an argument. And in most cases ...

  10. [CSS] Collapsing Margins

    Refactor the spacing between <header>, <article>, and <aside> so that elements wil ...