一个验证码的实现参考代码

Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class YanZhengMa : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
//  首先调用RndNum函数生成4位数字的验证码
        string str_ValidateCode = RndNum(4);
        
//  然后把它保存在用于验证的Session里边
        Session["imageCode"= str_ValidateCode;
        
//调用CreateImage函数,把验证码绘制到图片中
        CreateImage(str_ValidateCode);
    }
    
private string RndNum(int VcodeNum)
    {
        
string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
         
",q,r,s,t,u,v,w,x,y,z";
        
string[] VcArray = Vchar.Split(new Char[] { ',' });
        
string VNum = "";
        
//int temp = -1;
        Random rand = new Random();
        
for (int i = 1; i < VcodeNum + 1; i++)
        {

            
int t = rand.Next(36);

            VNum 
+= VcArray[t];
        }
        
return VNum;
    }
    
//生成随机颜色

    
private Color GetRandomColor()
    {
        Random RandomNum_First 
= new Random((int)DateTime.Now.Ticks);
        
//  对于C#的随机数,没什么好说的
        System.Threading.Thread.Sleep(RandomNum_First.Next(50));
        Random RandomNum_Sencond 
= new Random((int)DateTime.Now.Ticks);
        
//  为了在白色背景上显示,尽量生成深色
        int int_Red = RandomNum_First.Next(256);
        
int int_Green = RandomNum_Sencond.Next(256);
        
int int_Blue = (int_Red + int_Green > 400? 0 : 400 - int_Red - int_Green;
        int_Blue 
= (int_Blue > 255? 255 : int_Blue;
        
return Color.FromArgb(int_Red, int_Green, int_Blue);
    }
    
public void CreateImage(string str_ValidateCode)
    {
        
int int_ImageWidth = str_ValidateCode.Length * 13;
        Random newRandom 
= new Random();
        
//  图高20px
        Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
        Graphics theGraphics 
= Graphics.FromImage(theBitmap);
        
//  白色背景
        theGraphics.Clear(Color.White);
        
//  灰色边框
        theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 00, int_ImageWidth - 119);
        
//  10pt的字体
        Font theFont = new Font("Arial"10);
        
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
        {
            
string str_char = str_ValidateCode.Substring(int_index, 1);
            Brush newBrush 
= new SolidBrush(GetRandomColor());
            Point thePos 
= new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
            theGraphics.DrawString(str_char, theFont, newBrush, thePos);
        }
        
//  将生成的图片发回客户端
        MemoryStream ms = new MemoryStream();
        theBitmap.Save(ms, ImageFormat.Jpeg);

        Response.ClearContent(); 
//需要输出图象信息 要修改HTTP头 
        Response.ContentType = "image/Png";
        Response.BinaryWrite(ms.ToArray());
        theGraphics.Dispose();
        theBitmap.Dispose();
        Response.End();
    }

}

下面是使用代码

 

Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CRM._Default" %>

<!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>
<script type ="text/javascript">
    function UpdateImage()
    {
        var n
=Math.random();
        document.getElementById(
"image").src="System/CheckCode.aspx?r="+n;
    }
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
     
     
<img id ="image" src ="System/CheckCode.aspx" />
       
<a href ="javascript:UpdateImage()">看不清啊</a>
    
    
</div>
    
</form>
</body>
</html>


阅读全文并查看最新版:http://www.cckan.net/forum.php?mod=viewthread&tid=69

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值