验证码
来源:互联网 发布:nginx实现session共享 编辑:程序博客网 时间:2024/05/10 08:22
转自:http://www.cnblogs.com/hlxs/archive/2011/05/06/2087977.html
using System;using System.Drawing;namespace SecurityCode{ public class DrawMethod { /// <summary> /// 画图 /// </summary> /// <param name="content"></param> /// <param name="size"></param> /// <param name="fileName"></param> public void Draw(string content,Size size,string fileName) { Image image = new Bitmap(size.Width,size.Height); Graphics g = Graphics.FromImage(image); DrawBorder(g,image.Size); DrawContent(g,content,image.Size,(int)FontSet.Font.Size); DrawRandom(g, image.Size); image.Save(@"D:\\" + fileName); } /// <summary> /// 画边框 /// </summary> /// <param name="g"></param> /// <param name="size"></param> private void DrawBorder(Graphics g, Size size) { Pen pen = new Pen(SolidBrushSet.SolidBrush); Rectangle rect=new Rectangle(1,1,size.Width-4,size.Height-4); g.DrawRectangle(pen,rect); } //画字符串 private void DrawContent(Graphics g, string content,Size size,int fontHeight) { Point point = new Point(); int i = 0; point.Y = (size.Height - fontHeight) / 2; int distance = size.Width / (content.Length+1); foreach (char c in content) { point.X = i * distance + distance/2; g.DrawString(c.ToString(), FontSet.Font, SolidBrushSet.SolidBrush, point); i++; } } /// <summary> /// 画干扰 /// </summary> /// <param name="g"></param> /// <param name="size"></param> private void DrawRandom(Graphics g, Size size) { Pen pen = new Pen(SolidBrushSet.SolidBrush); Random rand = new Random(); for (int i = 0; i < 3; i++) { g.DrawLine(pen, rand.Next(size.Width), rand.Next(size.Width), rand.Next(size.Height), rand.Next(size.Height)); } for (int i = 0; i < 10; i++) { Rectangle rect = new Rectangle(rand.Next(2, size.Width - 2), rand.Next(2, size.Height - 2), 1, 1); g.DrawRectangle(pen, rect); } } }}using System;namespace SecurityCode{ /// <summary> /// 自动生成字符串 /// </summary> public class CreateContent { private string so = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM"; /// <summary> /// 生成内容 /// </summary> /// <returns></returns> public string GetContent() { Random rand = new Random(); string str = null; for (int i = 0; i < 6; i++) { str += so.Substring(rand.Next(62), 1); } return str; } }}using System;using System.Drawing;namespace SecurityCode{ /// <summary> /// 字体设置 /// </summary> public class FontSet { public static Font Font { get { FontFamily fFamily = GetFontFamily(); FontStyle fStyle = GetFontStyle(); int emSize = GetFontSize(); return new Font(fFamily, emSize, fStyle); } } private FontSet() { } /// <summary> /// 设置字体 /// </summary> private static FontFamily GetFontFamily() { Random rand = new Random(); return FontFamily.Families[rand.Next(FontFamily.Families.Length)]; } /// <summary> /// 设置字体样式 /// </summary> private static FontStyle GetFontStyle() { Random rand = new Random(); int index = rand.Next(1, 4); index = 1 << index; return (FontStyle)index; } /// <summary> /// 设置字体大小 /// </summary> public static int GetFontSize() { Random rand = new Random(); return rand.Next(12, 14); } }}using System;using System.Drawing;namespace SecurityCode{ /// <summary> /// 画笔设置 /// </summary> public class SolidBrushSet { /// <summary> /// 画笔 /// </summary> public static SolidBrush SolidBrush { get { Color color= GetColor(); return new SolidBrush(color); } } private SolidBrushSet(){} /// <summary> /// 随机生成画笔颜色 /// </summary> /// <returns></returns> public static Color GetColor() { Random rand = new Random(); int r = rand.Next(0,255); int g = rand.Next(0, 255); int b = rand.Next(0, 255); return Color.FromArgb(r,g,b); } }} private void Test() { DrawMethod drawMethod = new DrawMethod(); CreateContent createCont = new CreateContent(); for (int i = 0; i < 10; i++) { string content = createCont.GetContent(); Size size = new Size(100, 40); drawMethod.Draw(content, size, i.ToString() + ".jpg"); } }
- 简单验证码验证
- 验证验证码
- 验证码的验证
- 验证码的验证
- 验证码客户端验证
- java验证码 验证
- 验证码验证模块
- 手机短信验证码验证
- 验证验证码
- 表单验证+验证码
- thinkphp 验证码功能(验证验证码)
- 验证码
- 验证码
- 验证码
- 验证码
- 验证码
- 验证码
- 验证码
- GPU软件体系
- win7 x64 sp1系统在 VS 2010旗舰版下配置OpenCV 2.3.1
- 技术不好~紧张
- android 轮循的处理 Handler
- JVM terminated.Exit code = -1的错
- 验证码
- Hadoop1.0 Eclipse Plugin-作业提交
- 回调函数说明
- 改变电源开关频率来降低电磁干扰
- OpenGL中的FBO对象(含源码)
- Tape.tv获620万美元融资 开始全球扩张
- Android App 调用自带浏览器
- SQL教程
- 案例分析:基于消息的分布式架构