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类型并写在字符串后面. ③利用对话框显示生成的验证码,并提示用户输入验证码. ...
随机推荐
- android图片加水印,文字
两种方法: 1.直接在图片上写文字 String str = "PICC要写的文字"; ImageView image = (ImageView) this.findViewByI ...
- android 设计模式学习
1:单例模式 //对于创建开销较大的类可使用此方法,保证全局一个实例,在程序运行过程中该类不会因新建额外对象产生开销.示例代码如下:public class Singleton { private s ...
- PAT1001
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B Calculate a + b and output the sum in standard format 计算a+b ...
- HDFS在Linux下的命令
1.对hdfs操作的命令格式是 1.1hadoop fs -ls <path> 表示对hdfs下一级目录的查看 1.2 hadoop fs -lsr <path> 表示对hd ...
- 提升html5的性能体验系列之二列表流畅滑动
App的顶部一般有titlebar,下面是list.常见的一个需求是要在list滚动时,titlebar不动.这个简单的需求,实现起来其实并不简单. 在普通web上的做法是使用div的滚动条,把lis ...
- 一段代码详解JavaScript面向对象
(function(){ //私有静态成员 var user = ""; //私有静态方法 function privateStaticMethod(){ } Box = func ...
- 2016大连网络赛 Football Games
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- 未出现的子串(unapeared)
未出现的子串(unapeared) 题目描述 有一个长度为n的数字串,其中会出现数字1,2,3,…,q(5≤q≤9).现在的问题是,需要求出一个长度最小的串(出现的数字也是1-q),使得该串不是这个数 ...
- Myeclipse8.5中svn插件安装方法总结
[转]http://lwcheng1985.iteye.com/blog/696143 有改动 方法一:在线安装 1.打开HELP->MyEclipse Configuration Cent ...
- UML学习小结
最近在使用状态模式写一个仿Windows计算器的MFC程序,顺便学习了一下UML图的绘制,尤其是类图和状态图的绘制,这里做一下总结吧. 一.UML简介 统一建模语言UML(Unified Mod ...