马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现随机验证码

  [复制链接]
查看4030 | 回复3 | 2015-3-12 09:13:21 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using System.Drawing.Drawing2D;

using System.Drawing;

using System.IO;

using System.Drawing.Imaging;

 

public partial class ValidateCode : System.Web.UI.Page

{

    private string code;

    private const int ImageHeigth = 22;             //验证码图片的高度

    private const double ImageLengthBase = 12.5;    //验证码图片中每个字符的宽度

    private const int ImageLineNumber = 25;         //噪音线的数量

    private const int ImagePointNumber = 100;       //噪点的数量

    private int length;

    public static string VALIDATECODEKEY;       //此处用来保存验证码到Session的Key的名称

 

    //静态构造函数初始化验证码在Session中的Key名称

    static ValidateCode()

    {

        VALIDATECODEKEY = "VALIDATECODEKEY";

    }

 

    //设置验证码的长度和内容

    public ValidateCode()

    {

        this.length = 4;

        this.code = string.Empty;

    }

 

    /// <summary>

    /// 产生随机的验证码并加入Session

    /// </summary>

    /// <param name="length">验证码长度</param>

    /// <returns></returns>

    public string CreateCode(int length)

    {

        if (length <= 0)

        {

            return string.Empty;

        }

        Random random = new Random();

        StringBuilder builder = new StringBuilder();

        //产生随机的验证码并拼接起来

        for (int i = 0; i < length; i++)

        {

            builder.Append(random.Next(0, 10));

        }

        this.code = builder.ToString();

        this.Session[VALIDATECODEKEY] = this.code;

        return this.code;

    }

 

    /// <summary>

    /// 根据长度产生验证码

    /// 并将验证码画成图片

    /// </summary>

    /// <param name="length">验证码长度</param>

    public void CreateValidateImage(int length) 

    {

        this.code = this.CreateCode(length);

        this.CreateValidateImage(this.code);

    }

    /// <summary>

    /// 根据产生的验证码生成图片

    /// </summary>

    /// <param name="code">验证码</param>

    public void CreateValidateImage(string code) 

    {

        if (!string.IsNullOrEmpty(code))

        {

            this.Session[VALIDATECODEKEY] = code;

            //初始化位图Bitmap对象,指定图片对象的大小(宽,高)

            Bitmap image = new Bitmap((int)Math.Ceiling((double)(code.Length * ImageLengthBase)), ImageHeigth);

            //初始化一块画布

            Graphics graphics = Graphics.FromImage(image);

            Random random = new Random();

            try

            {

                int num5;

                graphics.Clear(Color.White);

                //绘制噪音线

                for (num5 = 0; num5 < ImageLineNumber; num5++)

                {

                    int num = random.Next(image.Width);

                    int num3 = random.Next(image.Height);

                    int num2 = random.Next(image.Width);

                    int num4 = random.Next(image.Height);

                    graphics.DrawLine(new Pen(Color.Silver), num, num3, num2, num4);

                }

                //验证码字体样式

                Font font = new Font("Tahoma", 12, FontStyle.Italic | FontStyle.Bold);

                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);

                //绘制验证码到画布

                graphics.DrawString(code, font, brush, (float)2, (float)2);

                //绘制噪点

                for (num5 = 0; num5 < ImagePointNumber; num5++)

                {

                    int x = random.Next(image.Width);

                    int y = random.Next(image.Height);

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

                }

                graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                MemoryStream stream = new MemoryStream();

                //保存图片

                image.Save(stream, ImageFormat.Gif);

                base.Response.ClearContent();

                base.Response.ContentType = "image/Gif";

                base.Response.BinaryWrite(stream.ToArray());

            }

            finally

            {

                graphics.Dispose();

                image.Dispose();

            }

        }

    }

    protected override void OnLoad(EventArgs e) 

    {

        this.CreateValidateImage(this.length);

    }

 

    // Properties

    public string Code

    {

        get

        {

            return this.Code;

        }

    }

    public int Length

    {

        get

        {

            return this.length;

        }

        set

        {

            this.length = value;

        }

    }

}
ibcadmin | 2015-3-12 09:18:30 | 显示全部楼层
.....顶一下
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
songyusb | 2015-3-12 09:29:15 | 显示全部楼层
我是路过的酱油哥
wibc | 2015-3-12 10:28:17 | 显示全部楼层
学习了
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则