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. ubuntu16 gitlab的简单安装

    1.安装好ubuntu的ssh服务,使用xshell登录虚拟机 2.下载安装包:  wget -c https://downloads-packages.s3.amazonaws.com/ubuntu ...

  2. python xss相关的编码解码小脚本

    1.功能分析: 实际工作中经常会遇到alert()之类的函数被防火墙过滤,而把alert()转化为ascii码放到String.fromCharCode()中就可以绕过,之前会一个一个查ascii表, ...

  3. SSH连接Linux操作:

    Centos6.5的操作: 1:需要下载一个Xshell连接工具: 2:在Linux输入ifconfig,查看IP地址, 3:使用Xshell连接 Ubuntu的操作: 1:需要下载一个Xshell连 ...

  4. AbstractRoutingDataSource 实现动态切换数据源

    扩展AbstractRoutingDataSource类 package com.datasource.test.util.database; import org.springframework.j ...

  5. 【备忘】ASP.NET MVC 5 升级到 ASP.NET Core MVC 的部分变化

    正在将一个 .net 4.5 的项目(MVC 5)升级到 .net core 2.1,中间遇到了许多的修改,记在下面,帮大家少走弯路. System.Drawing 下面很多类已经不存在(如Bitma ...

  6. 网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接

    本文原作者:“水晶虾饺”,原文由“玉刚说”写作平台提供写作赞助,原文版权归“玉刚说”微信公众号所有,即时通讯网收录时有改动. 1.引言 好多小白初次接触即时通讯(比如:IM或者消息推送应用)时,总是不 ...

  7. 简单粗暴的JavaScript笔记-1

    欢迎到我的简书查看我的文集 前言: 提示密码提示框 <head> <style> #div1 {width: 100px;height:50px;background:#FFC ...

  8. Ansible工具原理一

    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命 ...

  9. 运维笔记--ubuntu管理启动项

    启动项 https://blog.csdn.net/sz457763638/article/details/78182700

  10. MongoDB 生产环境笔记

    目录 MongoDB 生产环境笔记 一.vm.zone_reclaim_mode 参数 二.添加 swap 分区 三.设置 swappiness 参数 四.内核和文件系统版本 五.禁用 Transpa ...