Exemple #1
0
        /// <summary>
        /// 从颜色列表中随机选取颜色
        /// </summary>
        /// <param name="Color_L"></param>
        /// <returns></returns>
        private FontStyleExt GetColor(List <FontStyleExt> Color_L)
        {
            Random       rnd = new Random();
            int          i   = rnd.Next(0, Color_L.Count);
            FontStyleExt l   = Color_L[i];

            Color_L.RemoveAt(i);
            return(l);
        }
Exemple #2
0
        //将验证码生成图片显示
        public byte[] CreateCheckCodeImage(out string checkCode)
        {
            checkCode = this.GenerateCheckCode();
            Bitmap   image = new Bitmap(55, 20);
            Graphics g     = Graphics.FromImage(image);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            try
            {
                //生成随机生成器
                Random random = new Random();

                //清空图片背景色
                g.Clear(Color.White);

                //画图片的背景噪音线
                for (int i = 0; i < 8; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.FromArgb(random.Next(255), random.Next(255), random.Next(255))
                                       ), x1, y1, x2, y2);
                }

                StringFormat sf = new StringFormat();
                sf.Alignment     = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                List <FontStyleExt> a = GetColorList;
                for (int i = 0; i < checkCode.Length; i++)
                {
                    FontStyleExt Ftyle = GetColor(a);
                    Font         font  = new Font("Verdana", Ftyle.FontSize, (System.Drawing.FontStyle.Bold));
                    SolidBrush   brush = new SolidBrush(Ftyle.FontColor);
                    g.DrawString(checkCode.Substring(i, 1), font, brush, GetCodeRect(i), sf);
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                //Response.ClearContent();
                //Response.ContentType = "image/Gif";
                //Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
            return(ms.ToArray());
        }