博客
关于我
用户登陆的验证码的制作
阅读量:676 次
发布时间:2019-03-16

本文共 1811 字,大约阅读时间需要 6 分钟。

 

添加命名空间:

using System.Drawing;

 验证码的生成:

protected void FormCheck()

    {

        //////先得到验证码的内容并且存放到会话中

        Random rand = new Random();

        int len = rand.Next(4, 6);//随机获得验证码的长度4-6

        char[] str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();//将候选字符放入字符数组中

        System .Text .StringBuilder code=new System.Text.StringBuilder ();//用来保存显示的字符内容

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

        {

        code .Append (str [rand .Next (str.Length )]);

        }

        this.Session ["result"]=code.ToString () ;//将验证码存放到会话中

        ///////得到图像的大小

        Size imageSize = new Size();

        Font myFont = new Font("arial black", 20); //字体

        using (Bitmap bmp = new Bitmap(20, 15))

        { using (Graphics g=Graphics .FromImage (bmp ))

        {

         SizeF size=g.MeasureString (code.ToString () ,myFont,1000 );

            imageSize .Width =(int)size .Width +6;//得到高度和宽的

            imageSize .Height =(int)size .Height +5;

        }

        }

 

        /////创建验证码的图像

        using (Bitmap bmp=new Bitmap (imageSize .Width ,imageSize .Height ))

        {

        using (Graphics g=Graphics .FromImage (bmp ))

        {

            g.Clear(Color .White );

            g.DrawString  (code.ToString () ,myFont ,Brushes .Black ,new RectangleF (0,0,imageSize.Width ,imageSize .Height ));

        }

 

        int num = imageSize.Width * imageSize.Height * 30 / 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();

        }

    }

 

///

        /// 检查指定的文本是否匹配验证码
        ///
        ///
要判断的文本
        ///
是否匹配
        public static bool CheckCode( string text )
        {
        string txt = System.Web.HttpContext.Current.Session["checkcode"] as string ;
        return text == txt ;
        }

 

 

转载地址:http://ssdqz.baihongyu.com/

你可能感兴趣的文章
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql sysbench测试安装及命令
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
MySQL Troubleshoting:Waiting on query cache mutex
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
MySQL Workbench 数据库建模详解:从设计到实践
查看>>
MySQL Workbench 数据建模全解析:从基础到实践
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>