生成校验文字图片,如网页上的登录校验码(类:SubmitCoder)
生成校验文字图片,如网页上的登录校验码(类:SubmitCoder)
public enum SubmitCodeMode
{ Charater = 0, Number = 1, MixedMode = 2 } public class SubmitCoder { private Font _CodeFont; private Size _CodeImageSize = new Size(); private SubmitCodeMode _Mode = SubmitCodeMode.MixedMode; public SubmitCoder(Size codeImageSize, SubmitCodeMode mode, Font codeFont) { _CodeFont = codeFont; _CodeImageSize = codeImageSize; _Mode = mode; } /// <summary> /// build sumbitcode character /// </summary> public string GenerateCheckCode() { int CODE_LENGTH = 5; int number; char code; string checkCode = String.Empty; System.Random random = new Random(); int count = 1; while (true) { number = random.Next(); if (_Mode == SubmitCodeMode.MixedMode) { if (number % 2 == 0) code = (char)(’0’ + (char)(number % 10)); else code = (char)(’A’ + (char)(number % 26)); } else if (_Mode == SubmitCodeMode.Number) code = (char)(’0’ + (char)(number % 10)); else if (_Mode == SubmitCodeMode.Charater) code = (char)(’A’ + (char)(number % 26)); else continue; checkCode += code.ToString(); count += 1; if (count > CODE_LENGTH) break; } return checkCode; } /// <summary> /// build picture /// </summary> /// <param name="checkCode"></param> public Image BuildSumbitCodeImage(string checkCode) { //System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 36); System.Drawing.Bitmap image = new Bitmap(_CodeImageSize.Width, _CodeImageSize.Height); Graphics g = Graphics.FromImage(image); try { //build random number Random random = new Random(); //clean picture backcolor g.Clear(Color.White); //draw picture background yamp line for (int i = 0; i < 25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", _CodeFont.Size, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); //Font font = _CodeFont; System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //draw picutre foregound yamp dot for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); } //draw picture boderline g.DrawRectangle(new Pen(Color.Green), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); System.IO.Stream sr = ms; return System.Drawing.Image.FromStream(sr, true, true); } finally { g.Dispose(); image.Dispose(); } } }
参考文档:
C#图片加水印(Generate Watermark Picture) 检查文件是否图片格式 静态网页生成器 - HTML Generator Asp.Net防止恶意刷新网页 Asp.Net生成图片验证码(C/S框架网开源) C# 实现条码图片自动生成功能 C# Winform系统登录(登入/登出)架构设计 动态SQL生成器(Dynamic SQL Generator) C/S框架后台数据更新模型之ORM自动生成SQL基本原理 C#图片转换类支持按比例缩放图片 C# Winform 制作可任意拖动的不规则背景图片窗体 C#可拖动窗体(实现无标题栏的登录窗体) IE浏览器监控程序-监控IE窗体URL动态加载网页等信息 C# 下载Web网页指定URL图片并转换为Base64String格式 Dev ButtonEdit按钮设置图片和文字
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |