随机验证码

新建立一个生成验证码的页面
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;
using NewsService;

public partial class WebForm_ValidateCode : System.Web.UI.Page
{
    RandomNumImg RandImg = new RandomNumImg();
    protected void Page_Load(object sender, EventArgs e)
    {
        string checkCode = RandImg.CreateRanmondCode(4);

        Session["CheckCode"] = checkCode;

        CreateRamondImg(checkCode);
    }

    #region 产生随机图片
    /// <summary>
    /// 产生随机图片
    /// </summary>
    /// <param name="checkCode">随机数字</param>
    private void CreateRamondImg(string checkCode)
    {
        Bitmap btimage = new Bitmap(Convert.ToInt32(Math.Ceiling((decimal)(checkCode.Length * 14))), 22);

        Graphics g = Graphics.FromImage(btimage);

        try
        {
            Random Randmond = new Random();

            g.Clear(Color.AliceBlue);

            for (int i = 0; i < 25; i++)
            {
                int x1 = Randmond.Next(btimage.Width);

                int x2 = Randmond.Next(btimage.Width);

                int y1 = Randmond.Next(btimage.Height);

                int y2 = Randmond.Next(btimage.Height);

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

            Font font = new Font("comic sans ms", 12, FontStyle.Bold);

            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, btimage.Width, btimage.Height), Color.Blue, Color.DarkRed, 1.2f, true);

            g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);

            for (int i = 0; i < 100; i++)
            {
                int x = Randmond.Next(btimage.Width);

                int y = Randmond.Next(btimage.Height);

                btimage.SetPixel(x, y, Color.FromArgb(Randmond.Next()));
            }

            g.DrawRectangle(new Pen(Color.Silver), 0, 0, btimage.Width - 1, btimage.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            btimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

            Response.ClearContent();

            Response.ContentType = "image/gif";

            Response.BinaryWrite(ms.ToArray());


        }
        finally
        {
            g.Dispose();

            btimage.Dispose();
        }
    }

    #endregion
}

#region 产生随机数据
        /// <summary>
        /// 产生随机数据
        /// </summary>
        /// <param name="codeCount">产生随机数的个数</param>
        /// <returns>返回产生的随机数</returns>
        public string CreateRanmondCode(int codeCount)
        {
            string allChar = "0,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[] allCharArray = allChar.Split(',');

            string ranmodCode = "";

            int temp = -1;

            Random Ranmond = new Random();

            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    Ranmond = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }

                int t = Ranmond.Next(36);

                if (temp != -1 && temp == t)
                {
                    return CreateRanmondCode(codeCount);
                }
                temp = t;

                ranmodCode += allCharArray[t];
            }

            return ranmodCode;
        }

        #endregion


在需要用到验证码的页面添加一个<asp:Image>控件即可,但是要把ImageUrl指向生成验证码的页面

<asp:Image Runat="server" ID="ImageCheck" ImageUrl="ValidateCode.aspx"></asp:Image>


 

原文地址:https://www.cnblogs.com/VirtualMJ/p/524175.html