C#验证码公用类

  protected void GetCode()
      {
            System.Random rand = new Random();
            int len = 4;    //这里设置验证码长度,我的定义为4,最近见到些网上上有随机4,5,6位的。这里自己写个随机函数就可以了。 
            //char[] chars = "0123456789ABCDEFGHJKMNOPQRSTUVWXYZ".ToCharArray();
            char[] chars = "123456789ABCDEFGHJKMNPQRSTUVWXYZ".ToCharArray();
            System.Text.StringBuilder myStr = new System.Text.StringBuilder();

            for (int iCount = 0; iCount < len; iCount++)
            {
                myStr.Append(chars[rand.Next(chars.Length)]);
            }
            string text = myStr.ToString();
            // 保存验证码到 session 中以便其他模块使用 
            Session["code"] = text;   //这里你的登陆页面判断输入验证码是否正确,if (txtCode.text == Session["code"]) 正确读库检索User/Password,不正确弹窗。自己写
            Size ImageSize = Size.Empty;
            Font myFont = new Font("MS Sans Serif", 14, FontStyle.Bold);  //绘制在验证图片上的文字大小
            // 计算验证码图片大小 
            using (Bitmap bmp = new Bitmap(5, 5))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    SizeF size = g.MeasureString(text, myFont, 10000);
                    ImageSize.Width = (int)size.Width + 3;
                    ImageSize.Height = (int)size.Height + 3;
                }
            }
            // 创建验证码图片 
            using (Bitmap bmp = new Bitmap(ImageSize.Width, ImageSize.Height))
            {
                // 绘制验证码文本 
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.White);
                    using (StringFormat f = new StringFormat())
                    {
                        f.Alignment = StringAlignment.Near;
                        f.LineAlignment = StringAlignment.Center;
                        f.FormatFlags = StringFormatFlags.NoWrap;
                        g.DrawString(text, myFont, Brushes.Black, new RectangleF(0, 0, ImageSize.Width, ImageSize.Height), f);
                    }//using 
                }//using 
                // 制造杂点 杂点面积占图片面积的 20% 
                int num = ImageSize.Width * ImageSize.Height * 20 / 100;
                for (int iCount = 0; iCount < num; iCount++)
                {
                    // 在随机的位置使用随机的颜色设置图片的像素 
                    int x = rand.Next(ImageSize.Width);
                    int y = rand.Next(ImageSize.Height);
                    int r = rand.Next(255);
                    int g = rand.Next(255);
                    int b = rand.Next(255);
                    Color c = Color.FromArgb(r, g, b);
                    bmp.SetPixel(x, y, c);
                }//for 
                // 输出图片 
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                this.Response.ContentType = "image/png";
                ms.WriteTo(this.Response.OutputStream);
                ms.Close();
            }//using 
            myFont.Dispose();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值