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. 机器学习常用sklearn库

    Sklearn.model_selection(模型选择) Cross_val_score:交叉验证 Train_test_split:数据切割 GridsearchCV:网格搜索 Sklearn.m ...

  2. 简单 v.s. 基础

    无论做平面设计还是做摄影创作,其基础都是一些比较粗浅的看似毫无用处的简单技能.例如画直线.拍挂在墙上的电视机,不一而足. 同样的现象还能在web的前端设计中看到.一堆类似小孩学绘画的标签,几个可以更改 ...

  3. 1.ActionBar

    ActionBar低版本和高版本用法不同 低版本:1. 引用v7-appcompat2. Activity继承ActionBarActivity3. android:theme="@styl ...

  4. Java核心技术卷一基础知识-第6章-接口与内部类-读书笔记

    第6章 接口与内部类 本章内容: * 接口 * 对象克隆 * 接口与回调 * 内部类 * 代理 接口技术主要用来描述类具有什么功能,而并不给出每个功能的具体实现.一个类可以实现(implement)一 ...

  5. 使用《JAVA面向对象编程》总结

    抽象和封装 现实世界是“面向对象”的,面向对象就是采用“现实模拟” 的方法设计和开发程序. 面向对象设计是目前计算软件开发中最流行的技术.面向对象设计的过程就是抽象的过程. 类是对某一类事物的描述,是 ...

  6. 免费翻译API破解(简易翻译工具)

    思路:选取有道翻译,用fiddler抓取接口请求信息,提取相关请求参数,破解加密部分. 主要请求数据: i  :翻译文本 ts:时间戳 salt:ts +随机数 sign:加密信息,经过抓取信息,发现 ...

  7. vsftp搭建文档

    vsftpd端口的作用:控制连接:tcp21端口用于发送FTP命令数据连接:tcp20端口用于上传下载数据 传输模式:分为主动模式和被动模式主动模式是当需要传输数据时,客户端以PORT命令告知服务器, ...

  8. 【app】自动化环境搭建(Appium)for java

    Appium来做app自动化相信大家都很熟悉了吧,就不再赘述他的概念和作用了,我们接下来着重介绍怎么来搭建整个app自动化环境,整个环境包括如下几个步骤: 1.安装jdk和eclipse及配置jdk的 ...

  9. Source优化

    优化Source主要包括以下几方面: 1 优化sqlquery 在mapping中,通常会在Source Qualifier组件中使用sqlquery读取数据,则优化通常包括以下几点: 1)  如果在 ...

  10. python之集合(set)学习

    集合(set) 集合是一个无序的不重复元素序列,使用大括号({}).set()函数创建集合, 注意:创建一个空集合必须用set()而不是{},因为{}是用来创建一个空字典. 集合是无序的.不重复的.没 ...