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

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. Android Studio工程导入另一个工程作为lib

    简单视频应用开发时,使用Vitamio作为视频解码库,官方建议直接以工程作为lib方便升级,将该工程导入到项目时不知道该怎么做,参考了下面的博客,这里存档标记一下. 参考:导入一个Android St ...

  2. 在win64位,python64位2.7版本中安装pyHook

    今天看了一篇博文说的是利用pyhook监听键盘鼠标事件(感兴趣的可以看博客园中相关文章),文章中使用的pyHook模块的官方下载地址是:http://sourceforge.net/projects/ ...

  3. 关于win10安装VisualSVN遇到的一个问题及解决办法

    问题:在win10系统中安装VisaulSVN遇到问题,错误提示:There is problem with this Windows Installer package. A DLL require ...

  4. 射频识别技术漫谈(8)——动物标签【worldsing笔记】

    动物标签也是工作在TTF模式的ID(Identification)卡.之所以通常称为动物标签,估计是因为一来和识别人的ID卡相区分,二是因为动物不如人听话,人的ID卡可以做成卡片形状拿在手上,而动物不 ...

  5. MSSQLSERVER数据库- SP_EXECUTESQL的使用

    EXEC和SP_EXECUTESQL有什么区别呢? 1,它们之间最大的区别是嵌入式的参数,如下面一个语句 declare @sql nvarchar() declare @id varchar() ' ...

  6. Ecshop图片不清晰怎么办?

    很多人说缩略图的质量不高,模糊,那是因为gd库生成缩略图时,默认生成jpg缩略图或商品图的质量是75.可以通过修改生成缩略图质量的默认值来提高缩略图的质量. 找到includes/cls_image. ...

  7. Android v4 包和v7包问题

    昨天新建了一个android项目,加入了一个bootstrap的外部依赖和一个底部导航栏的外部依赖.结果jj 了,老是提醒我v4包v7包冲突: 事实是这样的,首先我的底部导航依赖库里面有一个v4包,那 ...

  8. 获取WMI硬件清单

    WMI服务能够报告详细的硬件信息.通常,每个硬件都来自它们自己的WMI代理类.但是要找出这些硬件类的名字是不容易. 所有硬件类都在同一个WMI根下面,你可以在根类查询所有的硬件: Get-WmiObj ...

  9. CentOS 6.5安装TortoiseSVN svn client

    TortoiseSVN: TortoiseSVN 是 Subversion 版本号控制系统的一个免费开源client,能够超越时间的管理文件和文件夹. 文件保存在中央版本号库,除了能记住文件和文件夹的 ...

  10. js 如何把JSON格式的字符串转换为JSON对象

    直接用eval函数.例:var str1 = '{ "url": "www.51qdq.com", "name": "js&quo ...