前台引用.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="YanZhengCode.aspx.cs" Inherits="YanZhengCode" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<img id="Image1" onclick="javascript:suiji();"src="YanZhengCode.aspx" alt="看不清楚,请单击我。" border="0"style="height:25px;width:60px;border: 0px; cursor: pointer;" />
<script language="javascript" type="text/javascript">
function suiji() {
document.getElementById("Image1").src = "gif_zl.aspx?chen=" + number;
}
</script>

</div>
</form>
</body>
</html>

YanZhengCode.cs

C#代码

using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System;

public partial class YanZhengCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string str = Request["chen"];
BuildCode();

//ShuImg();
}

private void BuildCode()
{

Color[] strcolor = { Color.Red, Color.Blue, Color.Brown, Color.Black, Color.Tomato };

string[] strfont = { "华文中宋" };
Random rands = new Random();
Bitmap images = new Bitmap(80, 26); //图片大小
Graphics g = Graphics.FromImage(images);
Font f = new Font(strfont[0], 20); //字体大小

//笔刷填充色
Brush b = new SolidBrush(Color.Tomato);
Brush b2 = new SolidBrush(strcolor[rands.Next(0, strcolor.Length)]);
Brush b3 = new SolidBrush(Color.Red);
Brush b4 = new SolidBrush(Color.FromArgb(128, Color.Green));
g.Clear(Color.Wheat);

//线条颜色
Pen p = new Pen(Color.Red, 0);
Pen p2 = new Pen(Color.Turquoise, 0);
Random rand = new Random();
for (int i = 0; i < 40; i++)
{
int y = rand.Next(images.Height);
int x = rand.Next(images.Width);
//g.DrawLine(p, 0, y, images.Width, y);
g.DrawRectangle(p2, x, y, 1, 1);
}
//int degree = 5;
//Point tempPoint = new Point();
//degree = ~degree + 1;
//g.RotateTransform(degree);
//tempPoint.X = 20;
//tempPoint.Y = 4;

g.DrawRectangle(p, 0, 0, images.Width - 1, images.Height - 1); //绘图区域
string strCode = rand.Next(1000, 10000).ToString();
Session["yanzhencode"] = strCode;
g.DrawString(strCode.Substring(0, 1), f, b, 2, -1);
g.DrawString(strCode.Substring(1, 1), f, b2, 19, -1);
g.DrawString(strCode.Substring(2, 1), f, b3, 37, -1);
g.DrawString(strCode.Substring(3, 1), f, b4, 50, -1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
images.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(ms.ToArray());
//Response.Cookies.Add(new HttpCookie("chne", "6780"));
g.Dispose();
images.Dispose();
}

private void ShuImg()
{
Response.ContentType = "image/*"; //设置发送头为图像数据
Response.Clear(); //清空发送数据
Random rand = new Random();
Stream stream = OpenFile(Server.MapPath("pic/" + rand.Next(1, 6).ToString() + ".jpg")); //打开文件流
Bitmap bit = new Bitmap(stream);
bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//保存到输出流中
stream.Close();
bit.Dispose();
Response.Flush(); //输出到客户端
}
}

**************************************二*******************

using System;  using System.IO;  using System.Drawing;  using System.Drawing.Imaging;  using System.Text;  using System.Collections;  using System.Web;  using System.Web.UI;  using System.Web.UI.WebControls;  using System.Web.UI.WebControls.WebParts;  using System.Web.UI.HtmlControls;  /**//// <summary>  ///   /// ** asp.net(C#) 生成验证码 **  ///   ///  File: GenerateCheckCode.aspx.cs  ///   ///  Author: 周振兴 (Zxjay 飘遥)  ///   ///  E-Mail: tda7264@163.com  ///   ///  Date: 07-04-10  ///   /// </summary>  public partial class GenerateCheckCode : System.Web.UI.Page  ...{      protected void Page_Load(object sender, EventArgs e)      ...{  string chkCode = string.Empty;  //颜色列表,用于验证码、噪线、噪点  Color[] color =...{ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };  //字体列表,用于验证码  string[] font =...{ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };  //验证码的字符集,去掉了一些容易混淆的字符  char[] character =...{ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };  Random rnd = new Random();  //生成验证码字符串  for (int i = 0; i < 4; i++)  ...{      chkCode += character[rnd.Next(character.Length)];  }  Bitmap bmp = new Bitmap(100, 40);  Graphics g = Graphics.FromImage(bmp);  g.Clear(Color.White);  //画噪线  for (int i = 0; i < 10; i++)  ...{      int x1 = rnd.Next(100);      int y1 = rnd.Next(40);      int x2 = rnd.Next(100);      int y2 = rnd.Next(40);      Color clr = color[rnd.Next(color.Length)];      g.DrawLine(new Pen(clr), x1, y1, x2, y2);  }  //画验证码字符串  for (int i = 0; i < chkCode.Length; i++)  ...{      string fnt = font[rnd.Next(font.Length)];      Font ft = new Font(fnt, 18);      Color clr = color[rnd.Next(color.Length)];      g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);  }  //画噪点  for (int i = 0; i < 100; i++)  ...{      int x = rnd.Next(bmp.Width);      int y = rnd.Next(bmp.Height);      Color clr = color[rnd.Next(color.Length)];      bmp.SetPixel(x, y, clr);  }  //清除该页输出缓存,设置该页无缓存  Response.Buffer = true;  Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);  Response.Expires = 0;  Response.CacheControl = "no-cache";  Response.AppendHeader("Pragma", "No-Cache");  //将验证码图片写入内存流,并将其以 "image/Png" 格式输出  MemoryStream ms = new MemoryStream();  try  ...{      bmp.Save(ms, ImageFormat.Png);      Response.ClearContent();      Response.ContentType = "image/Png";      Response.BinaryWrite(ms.ToArray());  }  finally  ...{      //显式释放资源      bmp.Dispose();      g.Dispose();  }      }  }

使用方法如下: 
新建名为 GenerateCheckCode.aspx 的文件,将上述代码拷贝到代码文件 GenerateCheckCode.aspx.cs 
在需要验证码的地方放置语句 <asp:Image ID="img1" runat="server" ImageUrl="~/GenerateCheckCode.aspx" /> 即可。

web验证码的更多相关文章

  1. 实现Web验证码图片-原理

    实现验证码的基础 GDI+ graphics device interface plus的缩写,即图形设备接口.GDI+为开发者提供了一组实现与各种设备(具有图形化能力但不涉及图形细节的设备)进行交互 ...

  2. java web 验证码-数字不变形

    controller代码: import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.a ...

  3. Web验证码图片的生成-基于Java的实现

    验证码图片是由程序动态产生的,每次访问的内容都是随机的.那么如何采用程序动态产生图片,并能够显示在客户端页面中呢?原理很简单,对于java而言,我们首先开发一个Servlet,这个Servlet的任务 ...

  4. Java web验证码——kaptcha的使用

    一.配置kaptcha的jar包 pom.xml配置: <-- 目前只有2.3.2版本--> <!-- https://mvnrepository.com/artifact/com. ...

  5. java web 验证码-字符变形(推荐)

    该文章转载自:http://www.cnblogs.com/jianlun/articles/5553452.html 因为在我做的这个系统中发现验证码有点偏上,整体效果看起来不太好,就做了一些修改. ...

  6. Java web验证码

    绘制验证码的主要步骤: 1,设置宽度高度,验证码个数,干扰线个数,可选字符,背景颜色,字体格式 2,画干扰线,随机生成颜色,字体,字符 3,设置缓冲区,得到画笔,设置边框,读写数据,存储图片. 1,S ...

  7. 从12306网站新验证码看Web验证码设计与破解

    2015年3月16日,铁路官方购票网站12306又出新招,在登录界面推出了全新的验证方式,用户在填写好登录名和密码之后,还要准确的选取图片验证码才能登陆成功.据悉,12306验证码改版后,目前所有抢票 ...

  8. web开发之web 验证码--- webqq 机器人

    一步一步来做WebQQ机器人-(一)(验证码) http://www.cnblogs.com/lianmin/p/4231340.html http://www.cnblogs.com/liulun/ ...

  9. java web 验证码 第一次不正确的问题,解决方案

    首先是form表单 ,获取图片验证码 然后使用js 去服务器验证 问题: 第一次明明输入正确 ,确验证不了??那是因为你在form表单发起请求 和 ajax  发起的请求  地址 中 一个使用127. ...

随机推荐

  1. 多线程中遇到ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);怎么解决

    XP下用VC开发的程序,在一个主线程调用3   个线程,线程之间要共享数据,结果总出现wincore.cpp   line   980   ASSERT(pMap-> LookupPermane ...

  2. 自动化测试CTS命令

    #!/sbin/sh chmod +x /system/bin/input i=0 while [ "$i" != "10" ] do #am instrume ...

  3. eclipse 添加jar包的方式

    参考资料地址:http://blog.csdn.net/mazhaojuan/article/details/21403717

  4. 遍历String字符串,得到出现次数最多的字母

    //There is no need to explain the code right? package com.hp.test; import java.util.HashMap; import ...

  5. DOM 节点属性

    DOM 节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeT ...

  6. R语言数据处理

    写在前面:数据处理是数据分析与挖掘必不可少的步骤.下面列出一些常用的数据处理操作. 一.类型转换 用class()查看数据的类型,用as.类型名()进行类型转换. > num <- as. ...

  7. python小程序之并发连接

    import threading import socket import time def conn(): cli = socket.socket() cli.connect(("58.6 ...

  8. js与objective-c的交互-备

    在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要从现在的网页跳到另外一个网页的时候,就会去修改 window.location.href 的位置:在我们的 Ob ...

  9. BIT_COUNT()和BIT_OR()

    在学习MySQL手册时,看到根据天数计算访问量时,出现了BIT_COUNT()和BIT_OR()两个函数来处理天数计算的问题 所使用的表格信息如下: mysql> select year,mon ...

  10. android的微信签名

    目标: 已经在微信官网申请了账号了,想要在上面开发应用,必须首先对应用进行审核.在审核之前,需要填写应用的相关信息,包括名称.图标.用途说明.签名等. 下面介绍如何获取程序的签名. 解决方案: 选择程 ...