asp.net 编写验证码
首先准备一个类来实现对验证码的绘制功能。
createcode.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Configuration; namespace LoginProject
{
public class createcode
{
static string yanzheng = "";
int length = ;
int fontsize = ;
string[] fontfamily = { "Arial Narrow", "Baskerville Old Face", "Algerian" };
Color[] colors = { Color.Red, Color.Pink, Color.Plum, Color.Purple, Color.PaleTurquoise, Color.Orchid };//颜色
string CodeValue = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,l,i,j,k,m,n,o,p,q,r,s,t,w,q,y,u,x,z"; //值
int imgwidth = ; //图片宽
int imgheight = ; //图片高
public static string YanZheng
{
get { return yanzheng; }
}
public int GetLength
{
get { return length; }
set { length = value; }
}
public int FontSize
{
get { return fontsize; }
set { fontsize = value; }
}
public string[] FontFamily
{
get { return fontfamily; }
set { fontfamily = value; }
}
public Color[] GetColor
{
get { return colors; }
set { colors = value; }
}
public string codevalue
{
get { return CodeValue; }
set { CodeValue = value; }
}
public int ImaHeight
{
get { return imgheight; }
}
public int ImaWidth
{ get { return imgwidth; } }
//产生随机字母
public string createvalue()
{
string val = "";
string[] arr = codevalue.Split(',');
Random rand = new Random();
for (int i = ; i < GetLength; i++)
{
int strindex = rand.Next(, arr.Length - );
val = val + arr[strindex];
}
yanzheng = val;
return val;
}
//绘制背景图像
public void DrawImage(Bitmap map)
{
Random rand = new Random();
for (int i = ; i < map.Width; i++)
{
for (int j = ; j < map.Height; j++)
{
if (rand.Next(, ) < )
{
map.SetPixel(i, j, Color.Beige);
}
}
}
}
//绘制验证码
public void DrawValue(string code, Bitmap bmap)
{
Random rand = new Random();
Graphics g = Graphics.FromImage(bmap);//获取背景图片(获取绘制器对象) System.Drawing.StringFormat sF = new StringFormat();
sF.Alignment = StringAlignment.Center;
for (int i = ; i < code.Length; i++)
{
System.Drawing.PointF point = new PointF(i * FontSize, );
string cellcode = code[i].ToString();
Font F = new Font(FontFamily[rand.Next(, )], FontSize, FontStyle.Italic);
g.DrawString(cellcode, F, Brushes.BlueViolet, point, sF);
}
}
//输出
public void Output(string code, HttpContext type)
{
// System.IO.MemoryStream ms = new MemoryStream();
type.Response.ContentType = "image/jpeg";
Bitmap bit = new Bitmap(ImaWidth,ImaHeight);
this.DrawImage(bit);
this.DrawValue(code, bit);
bit.Save(type.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//ms.Close();
//ms = null;
bit.Dispose();
bit = null;
}
}
}
然后准备一个页面来接收绘制出的验证码图片,如下
yanzhengma.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="yanzhengma.aspx.cs" Inherits="LoginProject.yanzhengma" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="imag" Width="100px" Height="50px" runat="server" ImageUrl="~/yanzhengma.aspx"/>
</div>
</form>
</body>
</html>
yanzhengma.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace LoginProject
{
public partial class yanzhengma : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
createcode code = new createcode();
string strcode = code.createvalue();
code.Output(strcode, this.Context);
}
}
}
如果想实现“看不清换一张”的功能则需要在另外使用一个接收页面。这个页面就是你的登陆页面代码如下
<script type="text/javascript">
function change() {
var img = document.getElementById("Image1");
img.src = img.src + '?';
}
</script> <td class="style1">验证码</td>
<td>
<asp:TextBox ID="yanzheng" runat="server" Width="150px" Height="25px"></asp:TextBox><label><img id="Image1" style=" Width:150px; Height:30px;" src="yanzhengma.aspx" alt="看不清换一张" onclick="change()"/></label>
</td>
当然还有其他的页面排版呀什么的,我就不一一写出来,就把最主要的部分写出来。
最后的效果,图片比较丑,主要还是个人的美工不好哇!

希望能帮到你哟..........
asp.net 编写验证码的更多相关文章
- ASP.NET MVC验证码演示(Ver2)
前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...
- 【转载】Asp.Net生成图片验证码工具类
在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...
- js编写验证码
这是一个简单的js编写的验证码,自己已经亲自验证,没有问题了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- ASP.NET图形验证码的生成
效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...
- ASP.NET——生成验证码
实现:随机生成四位数字的验证码,点击验证码可无刷新生成新的验证码,最后点击按钮进行检验 PS:本实例使用UpdatePanel实现无刷新. 前台代码: <asp:ScriptManager ID ...
- ASP.NET图片验证码
1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...
- ASP.NET图片验证码学习!
1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...
- ASP.NET MVC验证码演示
我们在网站登录或理一个评论时,可以放置一个验证码(Captcha),可以为系统免去那些恶意刷新等功能. 今次Insus.NET在asp.net mvc应用程序实现与演示验证码的产生以及应用等 . 前天 ...
- ASP.NET mvc 验证码 (转)
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
随机推荐
- SpringMVC日期参数自动绑定
Controller: @RequestMapping("/addUser") public String addUser(User user) { ... } Model: pu ...
- A Tour of Go Advanced Exercise: Complex cube roots
Let's explore Go's built-in support for complex numbers via the complex64 and complex128 types. For ...
- python 使用@property
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的 ...
- PT100测温函数
PT100电阻值计算过程如下: 理论电压关系为:V3-V1=11(V2-V1).由于电阻等的误差原因,采用实际测量求平均值的方法得出实际放大倍数. 放大电路测量几组数据如下:其中V3-V1=Av(V2 ...
- ios开发 json数据文件的存取
将Json存进本地文件夹 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomai ...
- stm32 堆和栈(stm32 Heap & Stack)【worldsing笔记】
关于堆和栈已经是程序员的一个月经话题,大部分有是基于os层来聊的. 那么,在赤裸裸的单片机下的堆和栈是什么样的分布呢?以下是网摘: 刚接手STM32时,你只编写一个 int main() ...
- 幻灯片(jquery实现)
html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- SignalTap II逻辑分析仪的使用
一.例子 我们使用如图1所示的verilog代码所实现的开关电路作为例子.这个电路把DE系列开发板上的前8个开关简单的和对应的8个红色LED相连接.它是这样工作的:在时钟(CLOCK_50)的上升沿读 ...
- ISE和Modelsim联合仿真(转)
相信很多人会遇到过这个问题,不知如何让ISE调用Modelsim进行仿真.我也迷糊了不少时间,查查找找,终于弄明白了,所以有了本文,和大家分享一下.我尽量讲得详细点儿,多多上图. 我的环境:Windo ...
- Stage3D学习笔记(三):使用GPU绘制一个图片
首先准备我们需要的图片,尺寸必须是2的幂数,我修改了一下Starling的图标拿来用: 还是先看看最终效果: 代码是居于上一节的代码进行修改的: package { import com.adobe. ...