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 ...
随机推荐
- JavaScript Iframe富文本编辑器中的光标定位
最近在项目中碰到一个比较棘手的问题: 在iframe富文本编辑器中,有个工具栏,这个工具栏在iframe标签之外,工具栏上有一个按钮,点击该按钮向iframe正在编辑中的光标处插入一个图片,图片会插入 ...
- CSS3圆角(border-radius)
CSS3中的border-radius支持IE9+,chrome,firefox 2.3+,以及safari3.2+浏览器. border-radius可直接使用,无需加前缀(注意:firefox13 ...
- Fizz Buzz
class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...
- CSS鼠标点击式变化图片透明度
今天分享前端代码主题:jequery控制css图片透明度 很多时候在网站图片处理上需要实现一些辅助效果,比如鼠标在图片上滑动时或点击时改变图片颜色(变灰或者其他),其实一个简单的办法就是改变图片css ...
- .Net Remoting浅释
面试的时候,被问到.Net Remoting 工作方式及它和Web Service的区别,在此做个整理,顺便回顾一下. .Net Remoteing: 我们可以认为.Net Remoting是一种分布 ...
- 随机List中数据的排列顺序
把1000个数随机放到1000个位置. 这也就是一个简单的面试题.觉得比较有意思.就顺带写一下 举个简单的例子吧. 学校统一考试的时候 有 1000个人,然后正好有 1000个考试位置,需要随机排列 ...
- JSP中使用的模式——JSP+Servlet+JavaBean
上一篇博文写到模式一:JSP+JavaBean 链接地址:http://wxmimperio.coding.io/?p=155 JSP中两种模式的总结 链接地址:http://wxmimperio.c ...
- spring与mybatis,strut2整合连接sqlserver不的不说的那点事儿
今天在通过spring与mybatis整合中,想连接下公司用的sqlserver数据库,结果使用Junit测发现没连上,于是就有了下面的问题: 准备工作都已经做好了 web中spring的监听配置了 ...
- bzoj 1079: [SCOI2008]着色方案 DP
1079: [SCOI2008]着色方案 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 803 Solved: 512[Submit][Status ...
- 1、Window10 Electron 开发环境搭建
1:首先安装nodejs C:\Users\yan>node -v v4.5.0 2:安装Electron C:\Users\yan>npm install --g electron-pr ...