c# 画布验证码
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# 画布验证码的更多相关文章
- GD图片(画布)的制作及验证码的应用
创建画布:创建画布的函数有 imagecreatetruecolor(width,height);创建画布 width指画布的宽height指画布的高 imagecolorallocate(img,r ...
- angular中使用canvas画布做验证码
//填充画布,五位随机数 drawNumber(): void { this.clearCanvas(); let ctx: CanvasRenderingContext2D = this.myGra ...
- ecshop验证码
<?php //仿制ecshop验证码(四位大写字母和数字.背景) //处理码值(四位大写字母和数字组成) //所有的可能的字符集合 $chars = 'ABCDEFGHIJKLMNOPQRST ...
- webform:图片水印、验证码制作
一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(16)-权限管理系统-漂亮的验证码
系列目录 我们上一节建了数据库的表,但我发现很多东西还未完善起来,比如验证码,我们先做好验证码吧,验证码我们再熟悉不过了,为了防止恶意的登录,我们必须在登录页面加入验证码,下面我将分享一个验证码,这个 ...
- android图片验证码--自绘控件
自绘控件的内容都是自己绘制出来的 大致流程如下: 1.定义一个类继承view 使用TypedArray初始化属性集合 在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件 ...
- webform(十)——图片水印和图片验证码
两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...
- 利用PHP绘图函数实现简单验证码功能
index.php __________________________________________________________________________________________ ...
- webform文件上传、图片水印、验证码
文件上传: 所用控件:FileUpload 使用时的思路: 1.判断用户是否选中了文件 FileUpload.FileName获取选中的文件名,判断长度,如果长度大于零就代表已经选择了文件 JS端:通 ...
随机推荐
- git配置全局用户名
点击右键 ,点击git bash here 在控制面板输入 git config --global user.name "xxx" git config --global user ...
- Docker基础知识介绍
本节内容 1. Docker概述 2. Docker的安装 3. Docker基本使用 4. Docker相关命令汇总 5. Docker概念理解 一 Docker概述 Docker是什么 ...
- Lesson 22 A glass envelope
Text My daughter, Jane, never dreamed of receiving a letter from a girl of her own age in Holland. L ...
- configSections必须是根节点下第一个节点
对webconfig文档进行配置,添加了一个节点configSectionS,添加时,我自个想,按节点的名称以字母排序,就放在appSettings节点下. 谁知,在程序运行时,出现了错误: 参考上面 ...
- 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器
[源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...
- vim常用命令行备忘总结
一 窗口切换 1 :sp 水平切换当前窗口 2 :vsp 垂直切换当前窗口 3 :clo 关闭活动窗口 4 : on 只保留活动窗口 5 : ctrl + w 在窗口间循环切换 ctrl + ...
- C语言那年踩过的坑--局部变量,静态变量,全局变量在内存中存放的位置
先看几个概念: 1.bss是英文block started by symbol的简称,通常是指用来存放程序中未初始化的全局变量的一块内存区域,在程序载入时由内核清0.bss段属于静态内存分配.它的初始 ...
- vue 自学笔记记录
vue 自学笔记(一): 知识内容: 安装vue ,创建vue实例,安装第一个组件,单项数据流 https://www.cnblogs.com/baili-luoyun/p/10763163.htm ...
- Excel 斜线表头制作方法
Excel 斜线表头制作方法
- Redis(3)---Redis事务
Redis事务 Redis 通过 MULTI .EXEC. DISCARD 和 WATCH 四个命令来实现事务功能. MULTI :标记一个事务块的开始. EXEC: 执行所有事务块内的命令. DI ...