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端:通 ...
随机推荐
- 为什么delete指针后指针设为null(已解答)
int *p;/*........*/delete p; p=null; 看代码的过程中,有这么一个疑问.删除了指针p,指针p既是不存在,怎么还能设置指针p为null呢?为什么还要设置为null呢? ...
- Python之旅Day15 Bootstrap与Django初识
Bootstrap初识 Bootstrap简介 Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目.(官网http://www.boo ...
- DelphiXE10.2.3——跨平台生成验证码图片
$("#img-code").bind( 'click', function () { $(this).attr('src','VerifyCode?t='+Math.random ...
- 如何在Linux(Ubuntu)上安装Redmine
费话就不多说了,本文主要描述如何在如何在Linux(Ubuntu)上安装Redmine.通过这篇文章你将了解如下内容. TL;DR 在Linux(Ubuntu)上安装Redmine的基本流程 配置ph ...
- H5新增属性contenteditable(不用JS,实现div模拟textarea高度自增长)
无意中看到一篇博客介绍了contenteditable这个属性——不需用JS,只需在div里加上contenteditable="true",即可实现div模拟textrarea( ...
- 边学边做,简单的 GraphQL 实例
项目中有功能要调用 API,对方 API 用的是 GraphQL 实现,就简单学了下,感叹技术进步真快,Facebook 发明的这玩意儿咋这么牛逼,使前端开发人员变得主动起来,想要什么接口.返回什么结 ...
- MQ 简单的使用
需要创建两个控制台应用 引用用下面的包 (1)生产者 static void Main(string[] args) { ConnectionFactory factory = new Connect ...
- Android主线程的消息系统(Handler\Looper)
前言: 之前的文章写的都是关于Bitmap和内存的优化技术,这一篇文章给大家谈谈Handler. Handler是Android系统中比较重要的一个知识,在Android多线程面试经常会被问到,在实际 ...
- "i=i++"在java等高级语言的解释
前言 最近学习我在的团队招人,面试.我出了一道c的题. int main(){ ; i+=++i; printf("%d",i); } 别喷!今晚学长们说,java,c#中完全不同 ...
- 【原创】Github团队协作之Pull请求
首先声明:Github上关于代码团队协作方式有很多种,这里只讲述Github上其中的一种代码团队协作方式. Pull请求(Pull request) 1 综述 协作者通过fork一个新的代 ...