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端:通 ...
随机推荐
- day_5字符串和列表的各种操作方法
字符串类型: 字符串的定义是可以有多种引号嵌套 定义字符串是以开头的引号然后匹配和第一个引号相同的引号,所以当字符串中间出现和第一个引号相同的引号就会出错,这个时候就可以选择别的引号进行创建字符串,或 ...
- Java-异常初步练习
案例一: package com.esandinfo; /** * 自定义一个Exception类 */ class MyCustomException extends RuntimeExceptio ...
- weblogic 与项目jar冲突解决方案 ITsm部署
部署时出现找不到类itims*****IMOType 时删除 2个fvsd-res-ws-1.0.ja,itims-fvsd-res-sync.jar jar包 里面的DeviceInfoPort ...
- Java学习笔记四:三目运算符与字符串连接符等
一 .三目运算符与自增自减 GitHub代码练习地址:https://github.com/Neo-ML/JavaPractice/blob/master/OperPrac02.java 条件运算符由 ...
- Python学习笔记【第九篇】:Python面向对象基础
Python语言中一切皆对象(类.属性.方法.........) 概念 面向对象编程:Object Oriented Programming 简称OOP 面向对象程序设计 面向对象和面向过程都是解决问 ...
- 使用FileZilla从Linux系统下载文件
需求:将Linux系统的的某个文件夹(里面包含文件夹和文件)下载到我Windows系统某个文件夹里 之前我使用xshell下载,但是通过 rz :上传sz:下载 命令中的sz命令,下载失败. 下载 c ...
- 常见排序算法整理(python实现 持续更新)
1 快速排序 快速排序是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行 ...
- 原生js的简单倒计时
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>倒计 ...
- npm jspdf报错
137073 warn optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.2 (node_modules\chokidar\node_modul ...
- ExternalException (0x80004005): 无法执行程序
今天更新系统上传到阿里云服务器,报错如下: “/”应用程序中的服务器错误. 组策略阻止了这个程序.要获取详细信息,请与系统管理员联系. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆 ...