web验证码
前台引用.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验证码的更多相关文章
- 实现Web验证码图片-原理
实现验证码的基础 GDI+ graphics device interface plus的缩写,即图形设备接口.GDI+为开发者提供了一组实现与各种设备(具有图形化能力但不涉及图形细节的设备)进行交互 ...
- java web 验证码-数字不变形
controller代码: import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.a ...
- Web验证码图片的生成-基于Java的实现
验证码图片是由程序动态产生的,每次访问的内容都是随机的.那么如何采用程序动态产生图片,并能够显示在客户端页面中呢?原理很简单,对于java而言,我们首先开发一个Servlet,这个Servlet的任务 ...
- Java web验证码——kaptcha的使用
一.配置kaptcha的jar包 pom.xml配置: <-- 目前只有2.3.2版本--> <!-- https://mvnrepository.com/artifact/com. ...
- java web 验证码-字符变形(推荐)
该文章转载自:http://www.cnblogs.com/jianlun/articles/5553452.html 因为在我做的这个系统中发现验证码有点偏上,整体效果看起来不太好,就做了一些修改. ...
- Java web验证码
绘制验证码的主要步骤: 1,设置宽度高度,验证码个数,干扰线个数,可选字符,背景颜色,字体格式 2,画干扰线,随机生成颜色,字体,字符 3,设置缓冲区,得到画笔,设置边框,读写数据,存储图片. 1,S ...
- 从12306网站新验证码看Web验证码设计与破解
2015年3月16日,铁路官方购票网站12306又出新招,在登录界面推出了全新的验证方式,用户在填写好登录名和密码之后,还要准确的选取图片验证码才能登陆成功.据悉,12306验证码改版后,目前所有抢票 ...
- web开发之web 验证码--- webqq 机器人
一步一步来做WebQQ机器人-(一)(验证码) http://www.cnblogs.com/lianmin/p/4231340.html http://www.cnblogs.com/liulun/ ...
- java web 验证码 第一次不正确的问题,解决方案
首先是form表单 ,获取图片验证码 然后使用js 去服务器验证 问题: 第一次明明输入正确 ,确验证不了??那是因为你在form表单发起请求 和 ajax 发起的请求 地址 中 一个使用127. ...
随机推荐
- (原)python中import caffe提示no module named google.protobuf.internal
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...
- JSON基础知识
1.什么是json • JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) • JSON 是轻量级的文本数据交换格式 ...
- js兼容性 - 动态删除script标签后 ,定义的函数是否执行
hello.js function hello(){ alert('hello'); } hello.html <!DOCTYPE html> <html lang="en ...
- dobbo学习
http://www.cnblogs.com/Javame/p/3632473.html 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以 ...
- DataTables获取表单输入框数据
$(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...
- URL传值中文乱码
url含有中文 先encodeURI(url)编码 获取之后再解码decodeURI //加密 var param = "itname=" + slRows.ITNAME + &q ...
- python安装setuptools和pip
今天需要写一个python导出excel的小程序.这里需要用到XlsxWriter模块,但是这个模块并没有安装,所以需要先下载该模块,然后才能在程序中使用.这里就需要安装模块,我选择使用pip.以下就 ...
- Binary Search Tree BST Template
Use one queue + size variable public class Solution { public ArrayList<ArrayList<Integer>&g ...
- 简述tcp协议对http性能的影响及优化
当网站服务器并发连接达到一定程度时,你可能需要考虑服务器系统中tcp协议设置对http服务器的影响. tcp相关延时主要包括: 1.tcp连接时建立握手: 2.tcp慢启动拥塞控制: 3.数据聚集的N ...
- 【转】Linux下socket keep alive讲解
[需求]不影响服务器处理的前提下,检测客户端程序是否被强制终了.[现状]服务器端和客户端的Socket都设定了keepalive属性.服务器端设定了探测次数等参数,客户端.服务器只是打开了keepal ...