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端:通 ...
随机推荐
- 急速安装lnmp 编译版本-wiki-shell脚本实现一键部署
shell脚本lnmp.sh 环境:centos 6.5 .64位 #!/bin/bash yum install -y nano vim wget wget http://www.atomicorp ...
- 28.TreeSet
与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的.在前一篇中详细讲解了TreeMap实现机制,如果客官详细看了这篇博文或者对TreeMap有比较详细的了解,那 ...
- JavaScript实现页面刷新滚动条位置不变(利用cookie)
实验环境:vs2015 asp.net(C#) 主要原理: 1.在页面滚动时或点击按钮时将当前滚动条位置记录到cookie[pos], 2.页面刷新或重载时查询cookie[pos]中的值是否存在,若 ...
- 监听HTTP请求
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Oracle.DataAccess.Client; using System; usi ...
- C++面试基础概念之动态库篇
编写DLL所学所思(1)——导出函数 编写DLL所学所思(2)——导出类 C++ DLL导出类 知识大全 C++类库开发详解
- 网络编程懒人入门(六):深入浅出,全面理解HTTP协议
本文引用了自简书作者“涤生_Woo”的文章,内容有删减,感谢原作者的分享. 1.前言 HTTP(全称超文本传输协议,英文全称HyperText Transfer Protocol)是互联网上应用最为广 ...
- 白话TCP为什么需要进行三次握手
阅读本文大概需要 2.3 分钟. 作者:雨林 https://www.cnblogs.com/yuilin 首先简单介绍一下TCP三次握手 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次 ...
- Metasploit Framework(7)客户端渗透(下)
文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 应用场景: Kali机器IP:192.168.163. ...
- 【Spark调优】聚合操作数据倾斜解决方案
[使用场景] 对RDD执行reduceByKey等聚合类shuffle算子或者在Spark SQL中使用group by语句进行分组聚合时,经过sample或日志.界面定位,发生了数据倾斜. [解决方 ...
- DFA算法实现关键字查找(正则原理入门)
前言:一直都这样认为“正则表达式是一个很有用的技能”,从一开始的磕磕绊绊的使用和摸索,到后来可以得心应手,这个过程离不来平时的不断学习和思考