ASP.NET with C#生成验证码的过程

生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。

具体生成验证码的代码如下,在生成随机字符串的同时会将字符串设置到一个Session["ValidateCode"] 中,实用的时候只要得到用户返回值和Session值比较就可以得出填入的验证码是否一致了。

 

using System;
using System.IO;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace QS_Web
{
    
namespace Kobold
    
{
        
public class Validate: Page
        
{
            
private void Page_Load(object sender, EventArgs e)
            
{
                
string strValidateCode = ValidateCode(4);//取得随机字符串,并设置Session值
                DrawValidateCode(strValidateCode,50,100);//绘图
            }

    
            
//绘图
            private void DrawValidateCode(string strValidateCode,int intFgNoise,int intBgNoise)
            
{
                
if(strValidateCode == null || strValidateCode.Trim() == String.Empty)
                
{
                    
return;
                }

                
else
                
{
                    
//建立一个位图文件 确立长宽
                    Bitmap bmpImage = new Bitmap((int)Math.Ceiling((strValidateCode.Length * 12.5)), 22);
                    Graphics grpGraphics 
= Graphics.FromImage(bmpImage);
    
                    
try
                    
{
                        
//生成随机生成器
                        Random rndRandom = new Random();
    
                        
//清空图片背景色
                        grpGraphics.Clear(Color.White);
    
                        
//画图片的背景噪音线
                        for(int i=0; i<intBgNoise; i++)
                        
{
                            
int int_x1 = rndRandom.Next(bmpImage.Width);
                            
int int_x2 = rndRandom.Next(bmpImage.Width);
                            
int int_y1 = rndRandom.Next(bmpImage.Height);
                            
int int_y2 = rndRandom.Next(bmpImage.Height);
    
                            grpGraphics.DrawLine(
new Pen(Color.Silver), int_x1, int_y1, int_x2, int_y2);
                        }

                        
//把产生的随机数以字体的形式写入画面
                        Font font = new Font("Arial"12, (FontStyle.Bold | FontStyle.Italic));
                        LinearGradientBrush brhBrush 
= new LinearGradientBrush(new Rectangle(00, bmpImage.Width, bmpImage.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
                        grpGraphics.DrawString(strValidateCode, font, brhBrush, 
22);
    
                        
//画图片的前景噪音点
                        for(int i=0; i<intFgNoise; i++)
                        
{
                            
int int_x = rndRandom.Next(bmpImage.Width);
                            
int int_y = rndRandom.Next(bmpImage.Height);
    
                            bmpImage.SetPixel(int_x, int_y, Color.FromArgb(rndRandom.Next()));
                        }

    
                        
//画图片的边框线
                        grpGraphics.DrawRectangle(new Pen(Color.Silver), 00, bmpImage.Width - 1, bmpImage.Height - 1);
    
                        MemoryStream memsMemoryStream 
= new MemoryStream();
                        bmpImage.Save(memsMemoryStream, ImageFormat.Gif);
                        Response.ClearContent();
                        Response.ContentType 
= "image/Gif";
                        Response.BinaryWrite(memsMemoryStream.ToArray());
                    }

                    
finally
                    
{
                        grpGraphics.Dispose();
                        bmpImage.Dispose();
                    }

                }

            }

    
            
//取得随机字符串,并设置Session值
            private string ValidateCode(int intLength)
            
{
                
int intNumber;
                
char chrCode;
                
string strValidateCode = String.Empty;
    
                Random rndRandom 
= new Random();
    
                
for(int i=0;i<intLength;i++)
                
{
                    intNumber 
= rndRandom.Next();
                    
if(intNumber % 2 == 0)
                    
{
                        chrCode 
= (char)('0' + (char)(intNumber % 10));//如果随机数是偶数 取余
                    }

                    
else
                    
{
                        chrCode 
= (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z]
                    }

                    strValidateCode 
+= chrCode.ToString(); 
                }

    
                Session[
"ValidateCode"= strValidateCode;//设置Session["ValidateCode"]
                
//Response.Cookies.Add(new HttpCookie("strValidateCode",strValidateCode));
    
                
return strValidateCode;
            }

        }

    }

}

功能基本上看注释可以看明白了,再介绍一下实用方法吧。只要建立一个aspx文件,然后在头加入:

<%@ Page Inherits="QS_Web.Kobold.Validate" %>

然后在需要显示验证码的地方加入如下代码:

 

<img src="Validate.aspx" border="0" />

就这样简单,Easy吧?呵呵

最后:批评建议记得告诉我!我要努力向上不负诸君企望~~

最最后,记得当时写这个代码的时候参考过不少文章,但我怎么找也找不回来了。虽说天下文章一大抄,但是我们要尊重原创者的!我google不到了。希望你知道告诉我。我会加上原作者的名字的。

posted @ 2006-07-31 09:06  KenBlove  阅读(412)  评论(1编辑  收藏  举报