ASP.NET實現驗證碼

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   sp   art   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;


namespace UI._1209
{
    public partial class 驗證碼 : System.Web.UI.Page
    {
        #region///自訂方法


        #region/// 產生驗證碼圖片   private void CreateCodeImage(string checkCode)
        /// <summary>
        /// 產生驗證碼圖片
        /// </summary>
        private void CreateCodeImage(string checkCode)
        {
            //判斷隨機碼是否存在
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            int width=(int)Math.Ceiling((checkCode.Length * 12.5));//根據驗證碼設定畫布的寬度


            //設定畫布的大小
            Bitmap btmImg = new Bitmap(width, 22);


            //建立畫布
            Graphics g = Graphics.FromImage(btmImg);


            try
            {
                    //建立隨機數
                Random random = new Random();
                //設定畫布的背景顏色
                g.Clear(Color.White);


                //設定背景雜音線
                for (int i = 0; i < 2; i++)
                {
                    int x1 = random.Next(btmImg.Width);
                    int y1 = random.Next(btmImg.Height);
                    int x2 = random.Next(btmImg.Width);
                    int y2 = random.Next(btmImg.Height);
                    g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
                }


                //設定字型
                Font font = new Font("宋體", 12f,FontStyle.Bold);


                //設定文字的漸層顏色
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, btmImg.Width, btmImg.Height), Color.Blue, Color.DarkRed, 1.2f, true);


                //建立文字
                g.DrawString(checkCode, font, brush, 2, 2);


                //設定前景噪音點
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(btmImg.Width);
                    int y = random.Next(btmImg.Height);


                    //隨機產生噪音點並設定顏色
                    btmImg.SetPixel(x, y, Color.FromArgb(random.Next()));


                }


                //設定圖片的邊框線
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, btmImg.Width - 1, btmImg.Height - 1);


                //建立記憶體流
                MemoryStream ms = new MemoryStream();


                //儲存
                btmImg.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);


                //清除流
                Response.ClearContent();


                //設定輸出資料流的類型
                Response.ContentType = "image/Gif";


                //將其寫入到輸出資料流
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //回收畫布資源
                g.Dispose();
                btmImg .Dispose();
            }
        }


        #endregion


        #region///產生驗證碼並將其儲存到Cookies中  private string CreateCheckCode()
        /// <summary>
        /// 產生驗證碼並將其儲存到Cookies中
        /// </summary>
        /// <returns>返回結果的字串</returns>
        private string CreateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;


            Random random = new Random();


            for (int i = 0; i < 4; i++)
            {
                number = random.Next();




                code = (char)(‘0‘ + (char)(number % 10));




                checkCode += code.ToString();
            }


            Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));


            return checkCode;
        }


        #endregion


        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            CreateCodeImage(CreateCheckCode());
        }
    }
}

ASP.NET實現驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.