【原】简单的asp.net验证码实现

创建一个页面,名称为VerificationCode.aspx

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace VerificationCodeDemo
{
    
public partial class VerificationCode : System.Web.UI.Page
    {
        
/// <summary>
        
/// 页面初始化
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            
string verificationCode = CreateRandomCode(4);
            Session[
"VerificationCode"= verificationCode;
            CreateImage(verificationCode);
        }

        
/// <summary>
        
/// 产生数字和字符混合的随机字符串
        
/// </summary>
        
/// <param name="i">随机字符的位数</param>
        
/// <returns>随机字符串</returns>
        private string CreateRandomCode(int i)
        {
            
string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            
char[] allCharArray = allChar.ToCharArray();
            
string randomCode = string.Empty;

            Random rand 
= new Random();

            
for (int j = 0; j < i; j++)
            {
                
int k = rand.Next(61);
                randomCode 
+= allCharArray.GetValue(k);
            }

            
return randomCode;
        }

        
/// <summary>
        
/// 生成图象验证码
        
/// </summary>
        
/// <param name="verificationCode">随机字符串</param>
        private void CreateImage(string verificationCode)
        {
            
int iwidth = (int)(verificationCode.Length * 11.5);
            Bitmap image 
= new Bitmap(iwidth, 20);
            Graphics g 
= Graphics.FromImage(image);
            Font f 
= new Font("Arial"10, FontStyle.Bold);
            Brush b 
= new SolidBrush(Color.Azure);                      // 字母白色
            g.Clear(Color.Brown);                                       // 背景灰色
            g.DrawString(verificationCode, f, b, 33);
            Pen blackPen 
= new Pen(Color.Black, 0);
            Random rand 
= new Random();
            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
            image.Save(ms, ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType 
= "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }
    }

}

再创建一个页面,名称为WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="VerificationCodeDemo.WebForm1" %>

<!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 language="javascript" type="text/javascript">
      function check() {
        var v 
= document.getElementById("image1");
        v.setAttribute(
'src''VerificationCode.aspx?' + Math.random());
      }
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
      
<!-- 直接点击图片或文字均刷新 -->
      
<img id="image1" src="VerificationCode.aspx" alt="验证码" onclick="this.src=this.src+'?'" />
      
<a href="javascript:check()">看不清楚</a>
    
</div>
    
</form>
</body>

</html> 

posted @ 2011-05-20 02:17  temptation  阅读(262)  评论(0编辑  收藏  举报