using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void lblColor1_Click(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            ColorDialog cd = new ColorDialog();
            DialogResult dr = cd.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                lbl.BackColor = cd.Color;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            Random ran = new Random();
            string code = "";
            ; i < ; i++)
            {
                , );
                code += c;
            }
            ;
            Bitmap bmp = null;
            if (!(int.TryParse(txtLineNum.Text, out lineNum) && ckLine.Checked))
            {
                VerifyCode vc = new VerifyCode();
                bmp = vc.Draw();
            }
            else
            {
                VerifyCode vc = new VerifyCode(ckLine.Checked, lblColor1.BackColor, lblColor2.BackColor, int.Parse(txtLineNum.Text), code);
                bmp = vc.Draw();
            }
            picImg.Image = bmp;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication3
{
    public class VerifyCode
    {
        private bool _line = false;

        public bool Line
        {
            get { return _line; }
            set { _line = value; }
        }

        private Color _color1 = Color.Blue;

        public Color Color1
        {
            get { return _color1; }
            set { _color1 = value; }
        }
        private Color _color2 = Color.DarkRed;

        public Color Color2
        {
            get { return _color2; }
            set { _color2 = value; }
        }
        ;

        public int LineNum
        {
            get { return _lineNum; }
            set { _lineNum = value; }
        }

        private string _code = "ABCD";

        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

        public VerifyCode()
        {
        }

        public VerifyCode(bool ckline, Color color1, Color color2, int lineNum, string code)
        {
            this.Line = ckline;
            this.Color1 = color1;
            this.Color2 = color2;
            this.LineNum = lineNum;
            this.Code = code;
        }

        public Bitmap Draw()
        {
            if (this.Line)
            {
                return DrawImg(this.Line);
            }
            else
            {
                return DrawImg();
            }

        }

        private Bitmap DrawImg()
        {
            Bitmap bmp = , );
            Graphics grap = Graphics.FromImage(bmp);
            grap.Clear(Color.White);

            Font font = , FontStyle.Regular);
            Rectangle r = , , bmp.Width, bmp.Height);
            LinearGradientBrush lgb = new LinearGradientBrush(r, this.Color1, this.Color2, 1.2f, true);
            grap.DrawString(, );
            grap.Dispose();
            return bmp;
        }

        private Bitmap DrawImg(bool ckline)
        {
            Bitmap bmp = , );
            Graphics grap = Graphics.FromImage(bmp);
            grap.Clear(Color.White);

            //一半的干扰线
            Random random = new Random();
            ; i < ; i++)
            {
                int x1 = random.Next(bmp.Width);
                int x2 = random.Next(bmp.Width);
                int y1 = random.Next(bmp.Height);
                int y2 = random.Next(bmp.Height);

                );
                );
                ) ?  :  - int_Red - int_Green;
                int_Blue = (int_Blue > ) ?  : int_Blue;
                grap.DrawLine(new Pen(Color.FromArgb(int_Red, int_Green, int_Blue)), x1, y1, x2, y2);
            }

            Font font = , FontStyle.Regular);
            Rectangle r = , , bmp.Width, bmp.Height);
            LinearGradientBrush lgb = new LinearGradientBrush(r, this.Color1, this.Color2, 1.2f, true);
            grap.DrawString(, );
            grap.Dispose();
            //一半的干扰点
            ; i < ; i++)
            {
                int x1 = random.Next(bmp.Width);
                int x2 = random.Next(bmp.Height);

                );
                );
                ) ?  :  - int_Red - int_Green;
                int_Blue = (int_Blue > ) ?  : int_Blue;

                bmp.SetPixel(x1, x2, Color.FromArgb(int_Red, int_Green, int_Blue));
            }

            return bmp;
        }
    }
}

效果

c# 画布验证码的更多相关文章

  1. GD图片(画布)的制作及验证码的应用

    创建画布:创建画布的函数有 imagecreatetruecolor(width,height);创建画布 width指画布的宽height指画布的高 imagecolorallocate(img,r ...

  2. angular中使用canvas画布做验证码

    //填充画布,五位随机数 drawNumber(): void { this.clearCanvas(); let ctx: CanvasRenderingContext2D = this.myGra ...

  3. ecshop验证码

    <?php //仿制ecshop验证码(四位大写字母和数字.背景) //处理码值(四位大写字母和数字组成) //所有的可能的字符集合 $chars = 'ABCDEFGHIJKLMNOPQRST ...

  4. webform:图片水印、验证码制作

    一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(16)-权限管理系统-漂亮的验证码

    系列目录 我们上一节建了数据库的表,但我发现很多东西还未完善起来,比如验证码,我们先做好验证码吧,验证码我们再熟悉不过了,为了防止恶意的登录,我们必须在登录页面加入验证码,下面我将分享一个验证码,这个 ...

  6. android图片验证码--自绘控件

    自绘控件的内容都是自己绘制出来的 大致流程如下: 1.定义一个类继承view 使用TypedArray初始化属性集合 在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件 ...

  7. webform(十)——图片水印和图片验证码

    两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...

  8. 利用PHP绘图函数实现简单验证码功能

    index.php __________________________________________________________________________________________ ...

  9. webform文件上传、图片水印、验证码

    文件上传: 所用控件:FileUpload 使用时的思路: 1.判断用户是否选中了文件 FileUpload.FileName获取选中的文件名,判断长度,如果长度大于零就代表已经选择了文件 JS端:通 ...

随机推荐

  1. jquery向Django后台发送数组

    在$.ajax中加入 traditional:true, //加上此项可以传数组 后端用 array = request.POST.getlist('ids') #django接收数组 来接收数组

  2. ICO图标下载地址

    http://findicons.com/ http://www.iconfont.cn/

  3. Openvswtich 学习笔记

    场景: 创建一个Virtual Switch,支持VLAN,支持MAC-Learning 包含下面四个Port: P1, truck port P2, VLAN 20 P3, P4 VLAN 30 包 ...

  4. Codeforces Round #554 (Div. 2) 1152A - Neko Finds Grapes

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces. ...

  5. Android逆向之静态分析

    想必打过CTF的小伙伴多多少少都触过Android逆向,所以斗哥将给大家整一期关于Android逆向的静态分析与动态分析.本期先带来Android逆向的静态分析,包括逆向工具使用.文件说明.例题解析等 ...

  6. Android P正式版即将到来:后台应用保活、消息推送的真正噩梦

    1.前言 对于广大Android开发者来说,Android O(即Android 8.0)还没玩热,Andriod P(即Andriod 9.0)又要来了.   下图上谷歌官方公布的Android P ...

  7. PHP实现大文件下载

    实现大文件下载的关键在于循环读取字节流 function downloadFile($filename) { //获取文件的扩展名 $allowDownExt = array ( 'rar', 'zi ...

  8. LabVIEW(五):DAQ同步

    1.在许多的测试测量应用当中,我们会需要在通过一个时间段内进行同步测量. 同步采集通常分为两类: (1).同时测量:即不同的任务在同一时刻开始.举例来说,我们会在一个模拟输入通道上采集数据,同时在一个 ...

  9. 【MySQL】percona-toolkit工具包

    [说明] percona-toolkit工具包同percona-xtrabackup一样都是用Perl写的工具包,percona-toolkit工具包是一组高级的管理mysql的工具包集,可以用来执行 ...

  10. .Net Core新建解决方案,添加项目引用,使用VSCode调试

    并不是我自己琢磨的,是看了别人学习的,因为写的都不完整,所以就整理一下记录后面忘了回看. 反正.Net Core是跨平台的,就不说在什么系统上了.假设我要建一个名为Doggie的解决方案,里面包含了一 ...