winfrom 底层类 验证码 分类: C# 2014-12-17 11:18 258人阅读 评论(0) 收藏
效果图:
底层类:
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="len">验证码长度</param>
/// <returns></returns>
private static string CreateRandomCode(int len) {
System.Random rand = new Random();
string randomCode = "";//随机码
char[] Allchar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'T', 'Q', 'W', 'X', 'Y', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 't', 'w', 'x',
'y', 'z' };
for (int i = 0; i < len; i++) {
//生成一个小于Allchar.Length的随机数
int a = rand.Next(Allchar.Length);
randomCode += Allchar[a].ToString();
}
//验证码
return randomCode;
}
/// <summary>
/// 生成验证码,填充到PictureBox中
/// </summary>
/// <param name="len">验证码长度</param>
/// <param name="pic">PictureBox名称</param>
/// <param name="revolve">旋转度数</param>
/// <param name="picHeight">图片高度</param>
/// <param name="picWidth">图片宽度</param>
/// <param name="charDistance">验证码中各个字符间距</param>
/// <param name="fontSize">验证码字符的大小</param>
public static string CreatePic(int len, PictureBox pic, int revolve, int picHeight, int picWidth, int charDistance, float fontSizes) {
//获取验证码
string strCode = CreateRandomCode(len);
//创建背景图片
Bitmap map = new Bitmap(picWidth, picHeight);
Graphics grap = Graphics.FromImage(map);
//清除画面,填充背景色为AliceBlue
grap.Clear(Color.AliceBlue);
//画一个矩形边框
grap.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);
//模式
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//画噪点
System.Random rand = new Random();
for (int i = 0; i < 50; i++) {
int x = rand.Next(0, map.Width - 1);
int y = rand.Next(0, map.Height - 1);
grap.DrawRectangle(new Pen(Color.Gray, 0), x, y, 1, 1);
}
//把字符串拆成单个字符
char[] chars = strCode.ToCharArray();
//文字居中
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
//定义颜色
Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange, Color.DarkCyan, Color.Purple };
//定义字体
string[] fonts = { "Arial", "Verdana", "Georgia", "Cambria Math", "华文中宋" };
for (int j = 0; j < len; j++) {
//颜色,字体的随机数,随机数作为下标
int cindex = rand.Next(colors.Length);
int findex = rand.Next(fonts.Length);
//字符颜色
Brush b = new System.Drawing.SolidBrush(colors[cindex]);
//字体样式、大小
System.Drawing.Font f = new System.Drawing.Font(fonts[findex], fontSizes, FontStyle.Bold);
//定义一个点
System.Drawing.Point p = new System.Drawing.Point(16, 20);
//转动的角度数
float angle = rand.Next(-revolve, revolve);
//移动光标到指定位置
grap.TranslateTransform(p.X, p.Y);
//转动angle度
grap.RotateTransform(angle);
//赋值
grap.DrawString(chars[j].ToString(), f, b, 1, 1, format);
//转动回去
grap.RotateTransform(-angle);
//移动光标到指定位置
grap.TranslateTransform(charDistance, -p.Y);
}
pic.Image = map;
pic.SizeMode = PictureBoxSizeMode.StretchImage;
return strCode;
}
前台调用:
说明:
(1)窗体要有一个PictureBox控件
(2)DataAccess 是底层类名称
DataAccess.CreatePic(4, pic, 50, 40, 100, 3, 20);
版权声明:本文为博主原创文章,未经博主允许不得转载。
winfrom 底层类 验证码 分类: C# 2014-12-17 11:18 258人阅读 评论(0) 收藏的更多相关文章
- 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 346人阅读 评论(0) 收藏
在百度编辑器示例代码基础上进行了修改,封装成类库,只需简单配置即可使用. 完整demo下载 版权声明:本文为博主原创文章,未经博主允许不得转载.
- iOS自定义字体及类目 分类: ios技术 2015-05-15 16:34 195人阅读 评论(0) 收藏
1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...
- C语言之void类型及void指针 分类: C/C++ 2015-07-13 11:24 8人阅读 评论(0) 收藏
原文网址:http://www.cnblogs.com/pengyingh/articles/2407267.html 1.概述 许多初学者对C/C 语言中的void及void指针类型不甚理解,因此在 ...
- Retinex系列之McCann99 Retinex 分类: 图像处理 Matlab 2014-12-03 11:27 585人阅读 评论(0) 收藏
一.McCann99 Retinex McCann99利用金字塔模型建立对图像的多分辨率描述,自顶向下逐层迭代,提高增强效率.对输入图像的长宽有 严格的限制,要求可表示成 ,且 ,. 上述限制来源于金 ...
- Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...
- Segment Tree with Lazy 分类: ACM TYPE 2014-08-29 11:28 134人阅读 评论(0) 收藏
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...
- 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- Poj 1029 分类: Translation Mode 2014-04-04 10:18 112人阅读 评论(0) 收藏
False coin Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16418 Accepted: 4583 Descr ...
随机推荐
- html中子div用了浮动怎样让父div的大小自动撑开(清除浮动)
浮动子div撑开父div的几种方法: (1)在父div中在添加一个清除浮动的子div<div style=" clear:both;"></div>,该di ...
- ListView复用和优化详解
我们每一个Android开发人员对ListView的使用肯定是很熟悉的,然而多少人能真正的懂ListView的缓存机制呢,说白了就是ListView为了提高效率,而内部实现的一种优化,牺牲一点内存.而 ...
- git推送失败的问题
git报错如下: fatal: 'origen' does not appear to be a git repositoryfatal: The remote end hung up unexpec ...
- Python 手册——解释器及其环境
错误处理: 有错误发生时,解释器打印一个错误信息和栈跟踪(监视)器?.交互模式下,它返回主提示符,如果从文件 输入执行,它在打印栈跟踪器后以非零状态退出.(异常可以由try语句中的except子句来控 ...
- c# 中List<T> union 深入理解
http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800202.html 借用 这个兄弟的代码 我就不献丑了 .我这里指记录下 public ...
- 学习Swift -- 构造器(上)
构造器(上) 构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.这个过程包含了为实例中的每个存储型属性设置初始值和为其执行必要的准备和初始化任务. 构造过程是通过定义构造器(Initi ...
- spring- properties 读取的五种方式
转至:http://www.cnblogs.com/hafiz/p/5876243.html 方式1.通过context:property-placeholder加载配置文件jdbc.properti ...
- VC6.0中重载操作符函数无法访问类的私有成员
整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...
- bzoj AC 50 庆祝~~
No. 1050 Solved 50 Submit 212 AC 60 PE 6 WA 88 TLE 13 MLE 5 OLE 2 RE 29 CE 9 10001002100310041005 10 ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...