C#中產生隨機驗證碼(摘抄)

來源:互聯網
上載者:User
public void MakeCheckCode(string checkCodeCookieName, int len)
        {
            //這兒定義你期待產生的隨機字母
            string[] arrLetter = new string[] { 
                                                   "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", 
                                                   "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9"
                                               };
            //隨機顏色
            Color[] arrColor = new Color[] { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Green, Color.Brown, Color.DarkCyan, Color.Blue };
            //隨機字型
            string[] arrFont = new string[] { "Arial", "Verdana" };

            Random romTemp = new Random();
            string strCheckCode = "";
            //產生隨機字串
            for (int i = 1; i <= len; i++)
            {
                strCheckCode = strCheckCode + arrLetter[romTemp.Next(arrLetter.Length)];
            }
            strCheckCode = strCheckCode.ToUpper();
            //將值寫入cook
            HttpCookie cookTemp = new HttpCookie(checkCodeCookieName);
            cookTemp.Values.Add("CheckCode", strCheckCode);
            this.Context.Response.Cookies.Add(cookTemp);
            int intWidth = strCheckCode.Length * 13; //計算圖片需要的寬度
            Bitmap bitmap1 = new Bitmap(intWidth,0x16);
            //繪圖
            Graphics graphics1 = Graphics.FromImage(bitmap1);
            graphics1.Clear(Color.White);
            for (int i = 0; i < 5; i++)
            {
                //隨機字母寬度
                int intLetterWidth = romTemp.Next(bitmap1.Width);
                //隨機字母高度
                int intLetterHeight = romTemp.Next(bitmap1.Height);
                graphics1.DrawRectangle(new Pen(Color.DarkGray, 0f), intLetterWidth, intLetterHeight, 1, 1);
            }
            for (int i = 0; i < strCheckCode.Length; i++)
            {
                int num7 = romTemp.Next(arrColor.Length);
                int num8 = romTemp.Next(arrFont.Length);
                Font font1 = new Font(arrFont[num8], 10f, FontStyle.Bold);
                Brush brush1 = new SolidBrush(arrColor[num7]);
                graphics1.DrawString(strCheckCode.Substring(i, 1), font1, brush1, (float) (3 + (i * 11)), 3f);
            }
            graphics1.DrawRectangle(new Pen(Color.LightGray, 0f), 0, 0, bitmap1.Width - 1, bitmap1.Height - 1);
            graphics1.Dispose();
            System.IO.MemoryStream stream1 = new System.IO.MemoryStream();
            bitmap1.Save(stream1, System.Drawing.Imaging.ImageFormat.Gif);
            this.Context.Response.ClearContent();
            this.Context.Response.ContentType = "image/Gif";
            this.Context.Response.BinaryWrite(stream1.ToArray());
            bitmap1.Dispose();
            graphics1.Dispose();
        }

聯繫我們

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