首先准备一个类来实现对验证码的绘制功能。

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 编写验证码的更多相关文章

  1. ASP.NET MVC验证码演示(Ver2)

    前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...

  2. 【转载】Asp.Net生成图片验证码工具类

    在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...

  3. js编写验证码

    这是一个简单的js编写的验证码,自己已经亲自验证,没有问题了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  4. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  5. ASP.NET——生成验证码

    实现:随机生成四位数字的验证码,点击验证码可无刷新生成新的验证码,最后点击按钮进行检验 PS:本实例使用UpdatePanel实现无刷新. 前台代码: <asp:ScriptManager ID ...

  6. ASP.NET图片验证码

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  7. ASP.NET图片验证码学习!

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  8. ASP.NET MVC验证码演示

    我们在网站登录或理一个评论时,可以放置一个验证码(Captcha),可以为系统免去那些恶意刷新等功能. 今次Insus.NET在asp.net mvc应用程序实现与演示验证码的产生以及应用等 . 前天 ...

  9. ASP.NET mvc 验证码 (转)

    ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...

随机推荐

  1. ADB 远程无线调试

    由于自己购买的x4412 Android开发板存在一个问题,是无法同时链接USB线,和插入无线网卡.只能使用其中一个功能 需要复现一个DRM在线下载的功能,同时需要抓起一些日志信息,此处就想到了使用 ...

  2. poj 2104 K-th Number(主席树)

    Description You are working for Macrohard company in data structures department. After failing your ...

  3. Magento 头部的演示信息去除

    进入后台: 系统-配置, 然后选择左栏的“设计”, 选择右栏的HTML头信息里面有个“Display Demo Store Notice”, 默认是yes,设置为“no”就可以了.

  4. mysql 处理中文乱码问题

    CREATE TABLE tbl_score( `ID` INT NOT NULL, `score` DEC(,) NOT NULL, `subject` VARCHAR() NOT NULL ); ...

  5. python pro practice

  6. homework01

    第一眼看到这个题目的时候就意识到这道题应该使用动态规划来解决,但因代码能力有限,因此从一维的问题开始解决,用C语言编写,代码如下: int maxsum(int *p,int size){ int i ...

  7. loadmore

    实例点击 loadmore.js /* * loadmore.js require jQuery,artTemplate * Butterfly 2013.08.28 */ define(['../u ...

  8. JavaScript的this简单实用

    1.默认绑定全局变量,在全局函数中: function fn(){ console.log(this.a); } var a=2; fn();//这里调用的是window 2.隐式绑定: functi ...

  9. GDB 入门篇

    调试流程:(使用gcc编译时加上 -g -Wall选项)gdb attach pidinfo bb filename:linenum / b filename:functionnamecp varia ...

  10. MFC版美女找茬

    今天心情:捡了个闲暇. 前几天工作出了个漏洞,电话会议时候怎么都是忽大忽小的声音,实在没听清电话会议的内容,完了依据想象交了一个设计方案,之后便是赋闲. 进入正题,美女找茬实现不难,没有设计上的难度, ...