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. Redhat/CentOS7-环境虚拟机简单搭建Nginx+Tomcat负载均衡集群

    Tomcat服务器是一个免费的开放源代码的web应用服务器,属于轻量级应用服务器,是开发和调试JSP程序的首选.由于Tomcat处理静态HTML的能力运不及Apache或者Nginx,所以Tomcat ...

  2. [转]OpenContrail 体系架构文档

    OpenContrail 体系架构文档 英文原文:http://opencontrail.org/opencontrail-architecture-documentation/ 翻译者:@KkBLu ...

  3. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

  4. 【sql注入教程】mysql注入直接getshell

    Mysql注入直接getshell的条件相对来说比较苛刻点把 1:要知道网站绝对路径,可以通过报错,phpinfo界面,404界面等一些方式知道 2:gpc没有开启,开启了单引号被转义了,语句就不能正 ...

  5. node.js使用免费的阿里云ip查询获取ip所在地

    在项目过程中,我们常常需要获取IP的所在地.而这一功能一般都是通过一些数据网站的对外接口来实现,这些接口一般情况下都是付费使用的.在这篇文章中我将记录,基于node.js的阿里云免费IP地址查询接口的 ...

  6. PHP新特性Trait

    Trait是PHP 5.4引入的新概念,看上去既像类又像接口,其实都不是,Trait可以看做类的部分实现,可以混入一个或多个现有的PHP类中,其作用有两个:表明类可以做什么:提供模块化实现.Trait ...

  7. Python学习笔记【第十三篇】:Python网络编程一Socket基础

    什么是⽹络 网络能把双方或多方连在一起的工具,即把数据从一方传递到另一方进行数据传递. 网络编程就是不同电脑上的软件能够进行数据传递.即进程间的通讯. 什么是TCP/IP协议 协议就是大家一起遵守的约 ...

  8. The SDK 'Microsoft.NET.Sdk' specified could not be found.

    有一台电脑用 VS Code 开发 .NET Core 项目时,每次打开文件夹都有一个错误(标题),定位在 C# 插件,鼠标放在代码上没有智能提醒,输入代码时没有补全提示,重装 VS Code 和所有 ...

  9. SpringBoot开源项目Jeeplatform

    JEEPlatform 一款企业信息化开发基础平台,可以用于快速构建企业后台管理系统,集成了OA(办公自动化).SCM(供应链系统).ERP(企业资源管理系统).CMS(内容管理系统).CRM(客户关 ...

  10. leetcode69 X的平方根的几种解法

    第一种自然就是调APi啦(手动滑稽) public int mySqrt(int x) { return (int)Math.sqrt(x); } 时间是52 ms,还超过了1/5的人呢 第二种 二分 ...