C#生成随机验证吗例子
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#生成随机验证吗例子的更多相关文章
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- 如何用jsp页面生成随机的验证数字码
checkNum.jsp <%@ page language="java" import="java.util.*,java.sql.*" pageEnc ...
- C#生成随机验证码
使用YZMHelper帮助类即可 using System; using System.Web; using System.Drawing; using System.Security.Cryptog ...
- C#产生不重复的随机数并生成随机文件名
本文转自:http://blog.ciznx.com/post/csharprandomnumberandrandomfilename.aspx 在项目中会遇到需要批量生成文件的时候,比如 asp.n ...
- struts---JSP界面验证码生成与验证
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- php生成随机产生六位数密码的代码
php生成随机产生六位数密码的代码,供大家学习参考.本文转自:http://www.jbxue.com/article/6199.html php生成随机产生六位数密码的代码,供大家学习参考. 复制代 ...
- .net生成随机验证码图片
/// <summary> /// 自定义图片验证码函数 /// 该函数将生成一个图片验证码,并将生成的code存放于Session["VerifyCode"]变量内. ...
- PHP 用session与gd库实现简单验证码生成与验证的类
验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的.目前大大小小的网站都有验证码.今天自己实现了一个简单的验证码类.说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下.当然,因为 ...
- 要求根据RandomStr.java:使用类型转换生成六位验证字符串,示例程序每次运 行时,都会生成不同的字符串。
1.程序设计思想验证码 ①定义一个字符串变量来保存随机生成的. ②利用循环产生六位随机数,在产生每一位时将其转换为char类型并写在字符串后面. ③利用对话框显示生成的验证码,并提示用户输入验证码. ...
随机推荐
- GameUnity 2.0 文档(四) 网格+四叉树 最优碰撞检测
在写之前,必须对 前一篇文档补充一下. Camera2DAngle类是 摄像机旋转 2d人物360度,PlayerMove是人物移动. 这两个类 都可以 360 ° 场景旋转人物也跟着旋转. 但不能 ...
- margin传递,子元素的上下margin会传递给父级
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- [JAR包] android引入JAR包,打包成JAR包,打包成Library项目,导入Library项目
(1)项目导入JAR包:1.在项目目录里建立一个libs目录,将外部jar包拷贝在里面.2.右键点击项目,Bulid Path->Configure Build Path3.在设置Libraie ...
- USACO Section 1.3 Ski Course Design 解题报告
题目 题目描述 有N座山,每座山都有一个高度,现在由于农夫想避税,所以想把这些山的高度进行一些改变,使得最高的山与最低的山之间的高度差不超过17.每座山最多只能改变一次高度,每次改变高度都会产生一定的 ...
- USER-AGENT是什么
USER-AGENT是什么? USER-AGENT:记录请求所来自的浏览器. User-Agent分析网站 http://www.useragentstring.com/ 通过解析User-Agent ...
- 区间gcd问题 HDU 5869 离线+树状数组
题目大意:长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种. 子段就是表示是要连续的a[] 思路:固定右端点,预处理出所有的gcd,每次都和i-1的gcd比较,然后 ...
- codeforces 369 div2 C dp
http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...
- Ubuntu vim显示行号语法高亮自动缩进
配置文件名为Ubuntu vimrc在Fedora中vim的配置文件存放在/etc目录中,配置文件名为Ubuntu vimrc在终端 输入以下命令来编辑Ubuntu vimrc配置文件:sudo vi ...
- Logcat过滤及常见用法整理
Usage: logcat [options] [filterspecs] options include:-s Set default filter to silent. ...
- apt
今天单元测试看到巨长字符串被加了很多引号,想起以前可以用跳脱符断行,试了不行,google说java7可以,我操. https://github.com/benelog/multiline/wiki/ ...