asp.net图形验证码 静态图形验证码

恶意注册问题,

解决办法:验证码....

验证码有很多种.

个人很多年前写过一种图形验证码.

 

其中第一种就是下边的代码

第二组 http://www.cnblogs.com/FayJack/articles/3063155.html

费话少说上代码

新建一个一般处理程序..即:.ashx文件  如下:CreateCode.ashx

<%@ WebHandler Language="C#" Class="CreateCode" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

public class CreateCode : IHttpHandler,IRequiresSessionState
{
//验证码 by Jack
    public void ProcessRequest(HttpContext context)
    {
        System.Web.HttpResponse response = context.Response;
        //验证码文字内容
        string checkCode = CreateCheckCodeString();
        context.Session.Add("createcodeStr", checkCode);
        
        int width = 80;//验证码图片宽度
        int height = 22;//验证码图片高度
        Font font = new Font("Arial", 12, FontStyle.Bold);//验证码字体
        SolidBrush brush = new SolidBrush(Color.Black);//用于写验证码的画笔
        Pen crosswise = new Pen(Color.Green, 0);//画横向干扰线的钢笔
        Pen vertical = new Pen(Color.FromArgb(255, 100, 100, 100), 0);//画纵向干扰线的钢笔
        Bitmap image = new Bitmap(width, height);//生成图像
        Graphics g = Graphics.FromImage(image);//生成一个绘画面板(画布)
        g.Clear(ColorTranslator.FromHtml("#f0f0f0"));//用指定颜色填充画布
        RectangleF rect = new RectangleF(5, 2, width, height);//定义文字的绘制矩形
        Random rand = new Random((int)DateTime.Now.Ticks);//生成干扰线的随机对象
        for (int i = 0; i < 2; i++)
        {
            Point start = new Point(0, rand.Next(height));
            Point end = new Point(width, rand.Next(height));
            g.DrawLine(crosswise, start, end);
        }
        for (int i = 0; i < 4; i++)
        {
            Point start = new Point(rand.Next(width), 0);
            Point end = new Point(rand.Next(width), height);
            g.DrawLine(vertical, start, end);
        }
        g.DrawString(checkCode, font, brush, rect);//将验证码写到画布上
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        try
        {
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            response.ClearContent();
            response.ContentType = "image/Gif";
            response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            ms.Flush();
            ms.Close();
            ms.Dispose();
            g.Dispose();
            image.Dispose();
        }
    }
    private string CreateCheckCodeString()
    { //定义用于验证码的字符数组
        char[] AllCheckCodeArray ={ '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 randomcode = "";
        Random rd = new Random();
        //生成4位验证码字符串
        for (int i = 0; i < 5; i++)
            randomcode += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)];
        return randomcode;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

 这样 随机 '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'

中的五个作为验证码存储在session里验证createcodeStr

 

页面里引用

 <img id="validCode" onclick="this.src='CreateCode.ashx?'+Math.random()" src="CreateCode.ashx"
                                style="cursor: pointer;" runat="server" title="点击刷新验证码" alt="点击刷新验证码" />

 

这里点击更换验证码.

后台验证的时候.

  string dd = HttpContext.Current.Session["Mind_createcodeStr"].ToString();
            if (dd.Equals(validatCode.ToUpper())){}

其中 validatCode 就是接收用户输入的验证码了.

或者是用户直接

 

当然 runat="server" 的服务器控件后台获取,或者是html表单提交后台request对象获取.

相信小白都能理解了....是不是太罗嗦?

 

升级版查看 http://www.cnblogs.com/FayJack/articles/3063155.html

posted @ 2013-05-06 17:13  张小凡  阅读(1055)  评论(1编辑  收藏  举报