天天看點

.Net圖形驗證碼

#region 操作驗證碼

        private void CreateImage(string codes)

        {

            try

            {

                label1.Text = codes;

                int iMapWidth = codes.Length * 21;

                Bitmap map = new Bitmap(iMapWidth, 28);     //建立圖檔背景  

                Graphics graph = Graphics.FromImage(map);

                graph.Clear(Color.AliceBlue);//清除畫面,填充背景  

                graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);//畫一個邊框  

                graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式 抗鋸齒  

                Random rand = new Random();

                //背景噪點生成  

                Pen blackPen = new Pen(Color.LightGray, 0);

                for (int i = 0; i < 50; i++)

                {

                    int x = rand.Next(0, map.Width);

                    int y = rand.Next(0, map.Height);

                    graph.DrawRectangle(blackPen, x, y, 1, 1);

                }

                //驗證碼旋轉,防止機器識别  

                char[] chars = codes.ToCharArray();//拆散字元串成單字元數組  

                //文字距中  

                StringFormat format = new StringFormat(StringFormatFlags.NoClip);

                format.Alignment = StringAlignment.Center;

                format.LineAlignment = StringAlignment.Center;

                //定義顔色  

                Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };

                //定義字型  

                string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };

                for (int i = 0; i < chars.Length; i++)

                {

                    int cindex = rand.Next(7);

                    int findex = rand.Next(5);

                    Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);//字型樣式(參數2為字型大小)  

                    Brush b = new System.Drawing.SolidBrush(c[cindex]);

                    graph.DrawString(chars[i].ToString(), f, b, i * 16 + 8, 15, format);

                }

                this.pictureBox1.Image = map;

            }

            catch (ArgumentException ex)

            {

                MessageBox.Show("建立圖檔驗證碼錯誤,錯誤原因:" + ex.Message.ToString());

            }

        }

        /// <summary>  

        /// 得到驗證碼  

        /// </summary>  

        /// <returns></returns>  

        private string getCode()

        {

            string code = "";

            int j;

            Random r = new Random();

            for (int i = 0; i < 4; i++)

            {

                j = r.Next();

                if (j % 3 == 0)

                {

                    code += (char)('A' + (char)(j % 26));

                }

                else

                {

                    code += (char)('0' + (char)(j % 10));

                }

            }

            return code;

        }

        /// <summary>  

        /// 加載驗證碼  

        /// </summary>  

        private void LoadCode()

        {

            string right_code = getCode();

            CreateImage(right_code);

        }

        #endregion