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 ...
随机推荐
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 【实习记】2014-08-22试用SSH客户端XShell与SecuretyCRT与MobaXterm总结
虚拟机下来了,是32位8G内存双核的win7系统. 测试显示实习生可以用办公机登录虚拟机在ssh到linux编译机.办公机虽ping通但不可以ssh上去. 只说这么多. 用惯linux下的sh ...
- Linux 信号量互斥编程
所谓信号量,其实就是一个数字.内核给这个数字赋予一定的含义,让它等于不同的值时所表示的意义不同.这样就可以用它来标示某种资源是否正被使用.信号的分类其实挺多的,主要还是二值和计数器.这里讨论二值 现在 ...
- 笔记一:Python的PyDev插件在eclipse上面安装(新的插件地址 location)
注:部分内容参考网上的,若有侵权,请作者联系我,马上进行删改 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Ec ...
- U盘安装ubuntu时出现的gfxboot.c32:not a COM32R image问题
方法特别简单:只需在提示后面输入 live 然后回车 就OK了
- 中文版Chrome浏览器不支持12px以下字体的解决方案
中文版Chrome浏览器不支持12px以下字体的解决方案 Chrome 27之前的中文版桌面浏览器会默认设定页面的最小字号是12px,英文版则没有限制,主要是因为chrome认为汉字小于12px就会增 ...
- MySQL配置文件路径及‘The total number of locks exceeds the lock table size’问题
在删除mysql中的数据时,遇到报错: ERROR 1206 (HY000): The total number of locks exceeds the lock table size 查了查,发现 ...
- bootstrap 下 标签页跳转总结
最近遇到一个问题,是关于bootstrap中的标签页实现上的一些功能实现,现总结一下. 问题描述:点击其他标签页后,如何在点击搜索按钮后自动跳转到第一个标签页.如下图 通过对bootstrap框架里的 ...
- WCF SOAP
WCF SOAP服务端解析 ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析. 本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Cor ...
- 各浏览器Cookie大小、个数限制
一.浏览器允许每个域名所包含的cookie数: Microsoft指出InternetExplorer8增加cookie限制为每个域名50个,但IE7似乎也允许每个域名50个cookie. Firef ...