簡單圖形驗證碼

來源:互聯網
上載者:User

      簡單的映像驗證碼程式

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string str_validateCode = GetRandomNumString(4);
                this.Session["ValidateCode"] = str_validateCode;
                createImage(str_validateCode);
            }
        }
        
    public static System.Drawing.Color GetRandomColor()
        {
            System.Random randomnum_first = new Random((int)DateTime.Now.Ticks);
            System.Threading.Thread.Sleep(randomnum_first.Next(50));
            System.Random randomnum_second = new Random((int)DateTime.Now.Ticks);
            int int_red = randomnum_first.Next(256);
            int int_green = randomnum_second.Next(256);
            int int_blue = (int_red + int_green) > 400 ? 0 : 400 - int_red - int_green;
            int_blue = (int_blue > 255) ? 255 : int_blue;
            return Color.FromArgb(int_red, int_green, int_blue);
        }
        public static void createImage(string str_validateCode)
        {
            int int_imageWidth = str_validateCode.Length * 15;
            Random random = new Random();
            System.Drawing.Bitmap theBitmap = new Bitmap(int_imageWidth, 20);
            System.Drawing.Graphics theGraphics = System.Drawing.Graphics.FromImage(theBitmap);
            theGraphics.Clear(Color.White);
            theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_imageWidth - 1, 19);
            System.Drawing.Font thefont = new Font("Arial", 10);
            for (int int_index = 0; int_index < str_validateCode.Length; int_index++)
            {
                string str_char = str_validateCode.Substring(int_index, 1);
                System.Drawing.Brush newbrush = new SolidBrush(GetRandomColor());
                System.Drawing.Point thepos = new Point(int_index * 13 + 1 + random.Next(3), 1 + random.Next(3));
                theGraphics.DrawString(str_char, thefont, newbrush, thepos);
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            System.Web.HttpContext.Current.Response.ClearContent();
            System.Web.HttpContext.Current.Response.ContentType = "image/Gif";
            System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            theGraphics.Dispose();
            theBitmap.Dispose();
            System.Web.HttpContext.Current.Response.End();
        }

    public static string GetRandomNumString(int n)
        {
            string yzm = string.Empty;
            for (int i = 0; i <= 9; i++)
            {
                yzm += i.ToString() + ",";
            }
            for (int j = 65; j <= 90; j++)
            {
                yzm += ((char)j).ToString() + ",";

            }
            for (int j = 97; j <= 122; j++)
            {
                yzm += ((char)j).ToString() + ",";
            }
            yzm = yzm.Substring(0, yzm.Length - 1);
            string[] varray = yzm.Split(',');
            string Vnum = string.Empty;
            int temp = -1;
            System.Random random = new Random();
            for (int i = 1; i <= n; i++)
            {
                if (temp != -1)
                {
                    random = new Random(i * temp * (int)DateTime.Now.Ticks);
                }
                int t = random.Next(varray.Length);
                if (temp != -1 && temp == t)
                {
                    return GetRandomNumString(n);
                }
                temp = t;
                Vnum += varray[t];

            }
            return Vnum;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.