博客
关于我
用户登陆的验证码的制作
阅读量:660 次
发布时间: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/

你可能感兴趣的文章
Android 四大组件、五大存储、六大布局总结
查看>>
【VRP问题】基于模拟退火改进遗传算法求解带时间窗含充电站的车辆路径规划问题EVRPTW
查看>>
【图像识别】基于模板匹配实现手写数字识别
查看>>
【语音去噪】最小二乘法(LMS)自适应滤波器matlab源码
查看>>
【边缘检测】基于CNN的灰度图像边缘提取matlab源码
查看>>
打工族有房有车,原来是这么实现的
查看>>
算法 顺序查找/折半查找/冒泡排序/选择排序(待改)
查看>>
华为1+X网络系统建设与运维(中级)——4.1 VLAN技术原理
查看>>
HDFS的学习积累
查看>>
Rancher入门到精通-2.0 systemctl 启动服务Connection timed out
查看>>
npm ERR! Failed at the node-sass@4.13.0 postinstall script.
查看>>
Rancher从入门到精通-2.0 配置gitlab代码库 404页面 原因有点扯
查看>>
ProgresSql 连接 ssl off 错误
查看>>
短视频SDK技术选型
查看>>
浏览器打开winscp 系统错误。代码:5。 拒绝访问。
查看>>
Github优秀项目推荐-SOFABolt 是蚂蚁金融服务集团开发的一套基于 Netty 实现的网络通信框架。
查看>>
Oracle Listener动态注册与静态注册(转载)
查看>>
MyBatis直接执行SQL查询及批量插入数据
查看>>
分布式锁与Transactional 导致锁失效
查看>>
Kubernetes 无法查询到并且无法删除pod实例的排查过程
查看>>