c#_生成图片式验证码
废话不多说直接上代码。
class Check_Code
{
/// <summary>
/// 生成随机验证码数字+字母
/// </summary>
/// <param name="codelen">验证码长度</param>
/// <returns>返回验证码</returns>
public static string MakeCode(int codelen)
{
if (codelen < )
{
return string.Empty;
}
int number;
StringBuilder strCheckCode = new StringBuilder();
Random random = new Random();
for (int index = ; index < codelen; index++)
{
number = random.Next();
if (number % == )
{
strCheckCode.Append((char)('' + (char)(number % )));//生成随机数字
}
else
{
strCheckCode.Append((char)('A' + (char)(number % )));//生成随机字母
}
}
return strCheckCode.ToString();
}
public static MemoryStream CheckCodeImage(string CheckCode)
{
if (string.IsNullOrEmpty(CheckCode))
{
return null;
}
Bitmap image = new Bitmap((int)Math.Ceiling((CheckCode.Length * 12.5)), );
Graphics graphic = Graphics.FromImage(image);//创建一个验证码图片
try
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = , y1 = , x2 = , y2 = ;
for (int index = ; index < ; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", , (FontStyle.Bold | FontStyle.Italic));//Font设置字体,字号,字形
//设置图形渐变色的起始颜色与终止颜色,渐变角度
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(CheckCode, font, brush, , );
int X = ; int Y = ;
//绘制图片的前景噪点
for (int i = ; i < ; i++)
{
X = random.Next(image.Width);
Y = random.Next(image.Height);
image.SetPixel(X, Y, Color.FromArgb(random.Next()));
}
//画图片的边框线
graphic.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
//将图片保存为stream流返回
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
graphic.Dispose();
image.Dispose();
}
}
}
生成验证码图片,需要先生成验证码,再将验证码处理为图片。
转载引用黎木博客-https://www.cnblogs.com/running-mydream/p/4071528.html
c#_生成图片式验证码的更多相关文章
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- Hyper-V初涉_共享式网络链接
任何一台计算机,如果不能与网络连通,可以说已经失去了大部分的功能,Windows 8尤是如此,虚拟机亦是如此. Hyper-V并不能对物理机的网卡进行识别,所以需要借助虚拟网卡通过物理机的网络共享实现 ...
- bootstrap_栅格系统_响应式工具_源码分析
-----------------------------------------------------------------------------margin 为负 使盒子重叠 等高 等高 ...
- python+selenium滑动式验证码解决办法
from selenium.webdriver import ActionChains action = ActionChains(driver) source=driver.find_element ...
- VC 静态库与动态库(三)动态库创建与使用_隐式链接
动态库分为二种,一种隐式链接,另一种显示调用.不论哪种动态库,本质都是运行时动态加载 隐式链接:程序运行时,由编译系统自动加载动态库,然后根据程序的引入表进行重定位,当程序退出时自动卸载动态库 显示调 ...
- 多测师讲解自动化测试 _如何解决验证码的问题_高级讲师肖sir
自动化测试如何解决验证码的问题对于web应用来说,大部分的系统在用户登录时都要求用户输入验证码,验证码的类型的很多,有字母数字的,有汉字的,甚至还要用户输入一条算术题的答案的,对于系统来说使用验证码可 ...
- Web前端_流式布局(百分比布局)
移动Web_流式布局(百分比布局) writer:late at night codepeasant 1(百分比布局) ☞核心知识点 1.流式布局(百分比布局) 2.视口设置 ☞今日目标 1. 能够使 ...
- sql优化_隐式-显示转换
======== 测试表1信息 =======SQL> select count(*) from tb_test; COUNT(*)---------- 3000000 SQL&g ...
- 2021年3月-第02阶段-前端基础-Flex 伸缩布局-移动WEB开发_流式布局
移动web开发流式布局 1.0 移动端基础 1.1 浏览器现状 PC端常见浏览器:360浏览器.谷歌浏览器.火狐浏览器.QQ浏览器.百度浏览器.搜狗浏览器.IE浏览器. 移动端常见浏览器:UC浏览器, ...
随机推荐
- linux下用ctrl+r快速搜索history命令
前提是,搜索已经使用的命令,否则是查不出来结果的. ctrl+r用途:反向搜索执行过的命令.(reverse-i-search) 1.任何目录下按住ctrl + r 2.输入历史命令中的字符串 ,比如 ...
- 【druid 】数据库连接池-sql解析
https://segmentfault.com/a/1190000008120254?utm_source=tuicool&utm_medium=referral sql解析 Druid 的 ...
- Luminar 3 for Mac(照片编辑工具)v3.1.0中文特别版
Luminar for Mac是一款多功能照片编辑软件,使用独特的AI工具加快速度,具备AI Sky Enhancer.Accent AI.太阳光线等创新功能.当然也保留了原有的功能,帮助你轻松的修复 ...
- MySQL Hardware--CentOS 6查看CPU信息
查看CPU信息 cpu_model=`cat /proc/cpuinfo |grep "model name"|awk -F ':' '{print $2}' |uniq` cpu ...
- R并行计算
# 参考文献: https://cosx.org/2016/09/r-and-parallel-computinghttps://blog.csdn.net/sinat_26917383/articl ...
- Mysql ssl 连接
在Azure创建了一个Mysql5.7服务,因为默认使用ssl连接,需要下载Azure的证书,并使用openssl生成客户端的证书.具体流程参考官方文档 大致步骤: 下载根证书, 安装openssl, ...
- QQ群成员发言次数统计(词云制作)
所用数据来自于之前的一篇博客: <QQ群成员发言次数统计(正则表达式版)> 链接:http://www.cnblogs.com/liyongzhao/p/3324026.html 1.首先 ...
- django template模板 母板 include导入
一,使用{% block name %}{% endblock %}定义一个模板,在模板页面中它的内容为空,在各页面用{% block name %}自己的标签内容{% endblock %}调用. ...
- CSS3 Vendor-prefixing
Browser vendors needed a way to add support for new features that were not yet standardized, but wit ...
- Opencv + opencv_contrib + Tesseract 之Qt开发环境搭建
1.软件包准备 opencv源码包地址: 官网 github opencv_contrib源码包地址: github Tesseract源码包地址: ...