X龙@China .Net 'blog

需要的不仅仅是工作,而是通过努力得来的美好将来。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

产生验证码ashx文件

Posted on 2011-06-16 15:06  X龙  阅读(271)  评论(0编辑  收藏  举报
<%@ WebHandler Language="C#" Class="VerifyPic" %>

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System .Web .SessionState ;
using System.IO;
using System.Drawing;

public class VerifyPic : IHttpHandler, IRequiresSessionState
{

    
public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType 
= "text/plain";
        
string temp = GenerateCheckCode();
        context.Session[
"code"= temp.ToLower();
        
this.CreateCheckCodeImage(temp, context);
    }

    
private string GenerateCheckCode()
    {
        
string chkCode = string.Empty;
        
//颜色列表,用于验证码、噪线、噪点  
        
//Color[] color ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
        
//字体列表,用于验证码  
        string[] font = { "Times New Roman""MS Mincho""Book Antiqua""Gungsuh""PMingLiU""Impact" };
        
//验证码的字符集,去掉了一些容易混淆的字符  
        char[] character = { '2''3''4''5''6''8''9''A''B''C''D''E''F''G''H''J''K''L''M''N''P''R''S''T''W''X''Y' };
        Random rnd 
= new Random();
        
//生成验证码字符串  
        for (int i = 0; i < 4; i++)
        {
            chkCode 
+= character[rnd.Next(character.Length)];
        }

        
return chkCode;
    }

    
private void CreateCheckCodeImage(string checkCode, HttpContext context)
    {
        
if (checkCode == null || checkCode.Trim() == String.Empty)
            
return;

        System.Drawing.Bitmap image 
= new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 15.5)), 22);
        Graphics g 
= Graphics.FromImage(image);

        
try
        {
            
//生成随机生成器
            Random random = new Random();

            
//清空图片背景色
            g.Clear(Color.White);

            
//画图片的背景噪音线
            for (int i = 0; i < 25; i++)
            {
                
int x1 = random.Next(image.Width);
                
int x2 = random.Next(image.Width);
                
int y1 = random.Next(image.Height);
                
int y2 = random.Next(image.Height);

                g.DrawLine(
new Pen(Color.Silver), x1, y1, x2, y2);
            }

            Font font 
= new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush 
= new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
            g.DrawString(checkCode, font, brush, 
22);

            
//画图片的前景噪音点
            for (int i = 0; i < 100; i++)
            {
                
int x = random.Next(image.Width);
                
int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            
//画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            context.Response.ClearContent();
            context.Response.ContentType 
= "image/Gif";
            context.Response.BinaryWrite(ms.ToArray());
        }
        
finally
        {
            g.Dispose();
            image.Dispose();
        }
    }

    
public bool IsReusable
    {
        
get
        {
            
return false;
        }
    }

}
点击这里给我发消息http://wp.qq.com/index.html