English Sentence Loading...
英语句子加载中...

验证码图片生成(4位字母与数字结合)

RandomImage.aspx.cs:

程序代码 程序代码
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Drawing;  
  
public partial class RandomImage : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        //输出带有随机验证码的图片  
        CreateCheckCodeImage(this.GenerateCheckCode());  
    }  
  
    /// <summary>  
    /// 随机生成4位随机字符(0-9)(A-Z)  
    /// </summary>  
    /// <returns></returns>  
    private string GenerateCheckCode()  
    {    
        //定义验证码长度  
        int CODELENGTH = 4;  
        int number;  
        string RandomCode = string.Empty;  
        Random r = new Random();  
        for (int i = 0; i < CODELENGTH; i++)  
        {  
            number = r.Next();  
            //字符从0-9,A-Z中随机生成,对应的ASCII码分别为48-57,65-90  
            number %= 36;  
            if (number < 10)  
            {  
                number += 48;  
            }  
            else  
            {  
                number += 55;  
            }  
            RandomCode += ((char)number).ToString();  
        }  
        //在Cookie中保存验证码  
        //Response.Cookies.Add(new HttpCookie("CheckCode",RandomCode));  
        Session["CheckCode"] = RandomCode;  
        return RandomCode;  
    }  
  
    /// <summary>  
    /// 输出包括4位随机数字的图片  
    /// </summary>  
    /// <param name="CheckCode"></param>  
    private void CreateCheckCodeImage(string CheckCode)  
    {    
        //若验证码为空,则直接返回  
        if (CheckCode == null || CheckCode.Trim() == string.Empty)  
        {  
            return;  
        }  
        //根据验证码的长度确定输出图片的宽度  
        int iWidth = (int)Math.Ceiling(CheckCode.Length*15m);  
        int iHeight = 20;  
        //创建图像  
        Bitmap image = new Bitmap(iWidth, iHeight);  
        //从图像获取一个绘图面  
        Graphics g = Graphics.FromImage(image);  
        try  
        {  
            Random r = new Random();  
            //清空图片背景色  
            g.Clear(Color.White);  
            //画图片的背景噪音线10条  
            for (int i = 0; i < 10; i++)  
            {  
                int x1 = r.Next(image.Width);  
                int x2 = r.Next(image.Width);  
                int y1 = r.Next(image.Height);  
                int y2 = r.Next(image.Height);  
                //用银色画出噪音线  
                g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);  
            }  
            //画图片的前景噪音点50个  
            for (int i = 0; i < 50; i++)  
            {  
                int x = r.Next(image.Width);  
                int y = r.Next(image.Height);  
                image.SetPixel(x, y, Color.FromArgb(r.Next()));  
            }  
            //画图片的框线  
            g.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);  
            //定义绘制文字的字体  
            Font f = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));  
            //线性渐变画刷  
            System.Drawing.Drawing2D.LinearGradientBrush brush = new    
  
System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue,    
  
Color.Purple, 1.2f, true);  
            g.DrawString(CheckCode, f, brush, 2, 2);  
            //创建内存流用于输出图片  
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())  
            {  
                //图片格式制定为png  
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);  
                //清除缓冲区流中的所有输出  
                Response.ClearContent();  
                //输出流的HTTP MIME类型设置为“image/Png”  
                Response.ContentType = "image/Png";  
                //输出图片的二进制流  
                Response.BinaryWrite(ms.ToArray());  
            }  
        }  
        finally  
        { //释放Bitmap对象和Graphics对象  
            g.Dispose();  
            image.Dispose();  
        }  
    }  
}  
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class RandomImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //输出带有随机验证码的图片
        CreateCheckCodeImage(this.GenerateCheckCode());
    }

    /// <summary>
    /// 随机生成4位随机字符(0-9)(A-Z)
    /// </summary>
    /// <returns></returns>
    private string GenerateCheckCode()
    {
        //定义验证码长度
        int CODELENGTH = 4;
        int number;
        string RandomCode = string.Empty;
        Random r = new Random();
        for (int i = 0; i < CODELENGTH; i++)
        {
            number = r.Next();
            //字符从0-9,A-Z中随机生成,对应的ASCII码分别为48-57,65-90
            number %= 36;
            if (number < 10)
            {
                number += 48;
            }
            else
            {
                number += 55;
            }
            RandomCode += ((char)number).ToString();
        }
        //在Cookie中保存验证码
        //Response.Cookies.Add(new HttpCookie("CheckCode",RandomCode));
        Session["CheckCode"] = RandomCode;
        return RandomCode;
    }

    /// <summary>
    /// 输出包括4位随机数字的图片
    /// </summary>
    /// <param name="CheckCode"></param>
    private void CreateCheckCodeImage(string CheckCode)
    {
        //若验证码为空,则直接返回
        if (CheckCode == null || CheckCode.Trim() == string.Empty)
        {
            return;
        }
        //根据验证码的长度确定输出图片的宽度
        int iWidth = (int)Math.Ceiling(CheckCode.Length*15m);
        int iHeight = 20;
        //创建图像
        Bitmap image = new Bitmap(iWidth, iHeight);
        //从图像获取一个绘图面
        Graphics g = Graphics.FromImage(image);
        try
        {
            Random r = new Random();
            //清空图片背景色
            g.Clear(Color.White);
            //画图片的背景噪音线10条
            for (int i = 0; i < 10; i++)
            {
                int x1 = r.Next(image.Width);
                int x2 = r.Next(image.Width);
                int y1 = r.Next(image.Height);
                int y2 = r.Next(image.Height);
                //用银色画出噪音线
                g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
            }
            //画图片的前景噪音点50个
            for (int i = 0; i < 50; i++)
            {
                int x = r.Next(image.Width);
                int y = r.Next(image.Height);
                image.SetPixel(x, y, Color.FromArgb(r.Next()));
            }
            //画图片的框线
            g.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);
            //定义绘制文字的字体
            Font f = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
            //线性渐变画刷
            System.Drawing.Drawing2D.LinearGradientBrush brush = new

System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue,

Color.Purple, 1.2f, true);
            g.DrawString(CheckCode, f, brush, 2, 2);
            //创建内存流用于输出图片
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                //图片格式制定为png
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                //清除缓冲区流中的所有输出
                Response.ClearContent();
                //输出流的HTTP MIME类型设置为“image/Png”
                Response.ContentType = "image/Png";
                //输出图片的二进制流
                Response.BinaryWrite(ms.ToArray());
            }
        }
        finally
        { //释放Bitmap对象和Graphics对象
            g.Dispose();
            image.Dispose();
        }
    }
}

      

index.aspx:

程序代码 程序代码
<img src="RandomImage.aspx" alt="验证码" id="imgRandom" />




文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 验证码
相关日志:

评论: 0 | 引用: 0 | 查看次数: 168
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: = 2 + 3  
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭