• C#图片验证码绘制


    制作验证码.也可以画弧线之类.....

    //新建一个网站,在后台代码中引用一个using Stystem.Drawing的命名空间
    
            Bitmap img = new Bitmap(100, 50); //制作一个宽100,高50的画板
            Graphics g = Graphics.FromImage(img); //画出背景图
            List<Color> clist = new List<Color>();//制作一个新的颜色集合
            clist.Add(Color.Red);//制作一个个的颜色内容
            clist.Add(Color.Yellow);
            clist.Add(Color.Blue);
            clist.Add(Color.Green);
            clist.Add(Color.Aqua);
            clist.Add(Color.Orange);
            clist.Add(Color.Pink);
            //验证码内容
            Random r = new Random();// 创作一个随机数
            string ss = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //写下所有关于验证码的数字跟字母
            string s = ""; //写一个空的字符串
            //防止字颜色与背景颜色重复
            Color bkcolor = clist[r.Next(0, clist.Count)];//挑选一个随机的颜色
            clist.Remove(bkcolor);//删除掉这个颜色
            g.FillRectangle(new SolidBrush(bkcolor), 0, 0, 100, 50); //设置背景颜色
            for (int i = 0; i < 4; i++)
            {
                s += ss[r.Next(0, ss.Length)]; //用for循环从ss中挑选4个验证码随机数
            }
            Font f = new Font("微软雅黑", 20); //设置字体大小
            Brush b = new SolidBrush(clist[r.Next(0, clist.Count)]); //用上面写的随机颜色定义一个字体颜色
            g.DrawString(s, f, b, 5, 5); //画出验证码
            Session["YZM"] = s; //设置一个全局验证码.用来验证是否正确
            for (var i = 0; i < 5; i++)
            {
                Pen pp = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]), r.Next(1, 3)); //设置一个随机颜色的画笔
                Point p1 = new Point(r.Next(0, 100), r.Next(0, 50)); //设置干扰线条的长短
                Point p2 = new Point(r.Next(0, 100), r.Next(0, 50)); //设置第二个干扰线条的长短
                g.DrawLine(pp, p1, p2); //画线
            }
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //画出这一张验证码

     忘了写怎么用了

    在新建一个网站

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>//用来验证图片里面的验证码
                <img id="imgyzm" src="YZM.aspx" />//这个就是刚刚制作好的验证码
                <asp:Button ID="Button1" runat="server" Text="验证" /> // 只是一个验证按钮
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>//用来显示是否正确

    在这个新建网站后台只需要写一个Button1的点击事件即可

     if (TextBox1.Text.ToUpper() == Session["YZM"].ToString().ToUpper())
                Label1.Text = "OK";
            else
                Label1.Text = "错误!!!!!!";
  • 相关阅读:
    springboot发送邮件
    事务(进程 ID 64)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品(转)
    Java8中的Stream API基本用法总结
    java时间API,SpringBoot中应用LocalDateTime(日期转换)
    springboot配置自定义消息转换器
    全文检索lucene
    springmvc总结(配置传递参数去除前后空格、参数绑定时处理日期)
    vs2015 安装问题汇总
    浏览器快捷方式被修改的元凶
    使用天平3次,从12个乒乓球找唯一1个轻重未知的废品
  • 原文地址:https://www.cnblogs.com/zJuevers/p/8287496.html
Copyright © 2020-2023  润新知