简单验证码


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
1,生成图片类:
using System;
using System.Configuration;
using Share.Data;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using Share.Utility;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace Web.Class.Constant
{
    /// <summary>
    /// Summary description for HelpConstant.
    /// </summary>
    public class VerifyCode : System.Web.UI.Page
    {
        private static string codenum;
        public static string CodeNum
        {
            get { return codenum; }
            set { codenum = value; }
        }
        public static string strRandom(int Ia, int Ib)
        {
            Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
            object i = rd.Next(Ia, Ib);
            return i.ToString();
        }
        public static byte[] GetCode(int Ia, int Ib)
        {
            CodeNum = strRandom(Ia,Ib);
            Bitmap bm = new Bitmap(50, 15, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Font f = new Font("Lucida Sans Unicode", 9, FontStyle.Bold);
            Graphics g = Graphics.FromImage(bm);
            Rectangle newRect = new Rectangle(0, 0, 50, 16);
            g.FillRectangle(new SolidBrush(Color.WhiteSmoke), newRect);
            g.DrawString(codenum, f, new SolidBrush(Color.Red), 1, -1);
            MemoryStream mStream = new MemoryStream();
            bm.Save(mStream, ImageFormat.Gif);
            g.Dispose();
            bm.Dispose();
            
            return mStream != null?mStream.ToArray():null;

        }

    }
}

2,输出:
        Response.ClearContent();
        Response.ContentType = "image/GIF";
        Response.BinaryWrite(VerifyCode.GetCode(10000, 99999));
        Response.End();
3,前台引用:
4,前台录入的与VerifyCode.CodeNum比较即可