C#生成随机验证码例子:

前端:

  <tr>
<td width="24%" height="" align="center" valign="top">
验证码:</td>
<td valign="top" width="37%" align="left">
<input type="text" name="txtCode" id="txtvalidateCode"/><a href="javascript:void(0)" id="valiateCode"><img src="/ashx/ValidateCode.ashx?id=1" id="imgCode" /></a><span style="font-size:14px;color:red " id="userCodeMsg"></span></td>
</tr>

给验证码图片绑定单击事件:

 $("#valiateCode").click(function () {
  $("#imgCode").attr("src",$("#imgCode").attr("src")+1);
});

后台生成验证码图片代码:

ValidateCode.ashx

 <%@ WebHandler Language="C#" Class="ValidateCode" %>

 using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState; public class ValidateCode : IHttpHandler, IRequiresSessionState
{
HttpContext context;
public void ProcessRequest (HttpContext context1) {
this.context = context1;
CreateCheckCodeImage(GenerateCheckCode());
} private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = ; i < ; i++)
{
number = random.Next(); if (number % == )
code = (char)('' + (char)(number % ));
else
code = (char)('' + (char)(number % ));
//code = (char)('A' + (char)(number % 26)); checkCode += code.ToString();
} //添加Session值
context.Session.Add("vCode", checkCode);
return checkCode;
} private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), );
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random(); //清空图片背景色
g.Clear(Color.White); //画图片的背景噪音线
for (int i = ; i < ; i++)
{
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);
} Font font = new System.Drawing.Font("Arial", , (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, , ); //画图片的前景噪音点
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.Silver), , , image.Width - , image.Height - ); System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} public bool IsReusable {
get {
return false;
}
} }

验证码效果:

C#生成随机验证码例子的更多相关文章

  1. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  2. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  3. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  4. Python使用PIL模块生成随机验证码

    PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...

  5. pillow实例 | 生成随机验证码

    1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...

  6. Django中生成随机验证码(pillow模块的使用)

    Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...

  7. struts2生成随机验证码图片

    之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...

  8. C#生成随机验证码

    使用YZMHelper帮助类即可 using System; using System.Web; using System.Drawing; using System.Security.Cryptog ...

  9. js用正则表达式验证用户和密码的安全性,生成随机验证码

    制作了一个表单,表单验证用户.密码.随机验证码 html页面

随机推荐

  1. PHP中全局变量的使用global和$GLOBALS[]

    From: http://blog.csdn.net/happyqyt/article/details/7219889 用PHP开发项目,不可避免的会使用到全局变量,比如一些网站的配置信息,全站通用, ...

  2. lakala GradientBoostedTrees

    /** * Created by lkl on 2017/12/6. */ import org.apache.spark.mllib.evaluation.BinaryClassificationM ...

  3. 设置wetty不需要账号登录便可进行命令行操作

    前一篇随笔我们将了Linux怎么安装部署Wetty服务,但是我们看到,在浏览器中输入http://127.0.0.1:3000进行访问的时候,还需要我们输入账号密码进行认证(如下图第一行所示). 但在 ...

  4. 小程序实现textarea随输入的文字行数变化高度自动增加

    参考链接:https://blog.csdn.net/liuwengai/article/details/78987957 该实现方法是根据上面的链接改编为小程序的实现,代码如下: wxml: < ...

  5. koa中上传文件到阿里云oss实现点击在线预览和下载

    比较好的在线预览的方法: 跳转一个新的页面,里面放一个iframe标签,或者object标签 <iframe src="xxx"></iframe> < ...

  6. 新手windows安装nginx

    windows安装nginx,下载地址:http://nginx.org/download/ 下载的时候,下载 .zip 后缀的压缩包,因为 .zip 的压缩包有nginx.exe 启动文件,其他没有 ...

  7. IBM Personal Communications 软件:精简绿色版TN3270终端模拟器:经测试可以在 (winxp、win2003、win764)上运行

    全系列版本啊!耗费了我大量的时间和精力,深夜熬到3点! 注意:如何使用?解压后,里面有个reg注册表文件,以管理员权限用户双击加载即可. http://files.cnblogs.com/files/ ...

  8. difference between TotalFreeSpace and AvailableFreeSpace

    Refer:http://stackoverflow.com/questions/7275806/what-is-the-difference-between-totalfreespace-and-a ...

  9. RSS 使用前详解

    您应当具备的基础知识 在继续学习之前,您需要对下面的知识有基本的了解: HTML / XHTML XML / XML 命名空间 什么是 RSS? RSS 指 Really Simple Syndica ...

  10. DTD与模式

    摘要 我们在制作页面时必须要测的就是IE浏览器,毕竟IE浏览器市场占有率还是很高.随着HTML5的流行,可能项目要求兼容IE最低版本为IE8或者更高,但是还是有很多项目兼容IE低版本.所以我们经常会碰 ...