废话不多说直接上代码。

     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#_生成图片式验证码的更多相关文章

  1. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  2. Hyper-V初涉_共享式网络链接

    任何一台计算机,如果不能与网络连通,可以说已经失去了大部分的功能,Windows 8尤是如此,虚拟机亦是如此. Hyper-V并不能对物理机的网卡进行识别,所以需要借助虚拟网卡通过物理机的网络共享实现 ...

  3. bootstrap_栅格系统_响应式工具_源码分析

    -----------------------------------------------------------------------------margin 为负 ​使盒子重叠 ​等高 等高 ...

  4. python+selenium滑动式验证码解决办法

    from selenium.webdriver import ActionChains action = ActionChains(driver) source=driver.find_element ...

  5. VC 静态库与动态库(三)动态库创建与使用_隐式链接

    动态库分为二种,一种隐式链接,另一种显示调用.不论哪种动态库,本质都是运行时动态加载 隐式链接:程序运行时,由编译系统自动加载动态库,然后根据程序的引入表进行重定位,当程序退出时自动卸载动态库 显示调 ...

  6. 多测师讲解自动化测试 _如何解决验证码的问题_高级讲师肖sir

    自动化测试如何解决验证码的问题对于web应用来说,大部分的系统在用户登录时都要求用户输入验证码,验证码的类型的很多,有字母数字的,有汉字的,甚至还要用户输入一条算术题的答案的,对于系统来说使用验证码可 ...

  7. Web前端_流式布局(百分比布局)

    移动Web_流式布局(百分比布局) writer:late at night codepeasant 1(百分比布局) ☞核心知识点 1.流式布局(百分比布局) 2.视口设置 ☞今日目标 1. 能够使 ...

  8. sql优化_隐式-显示转换

    ========  测试表1信息   =======SQL> select count(*) from tb_test; COUNT(*)----------   3000000   SQL&g ...

  9. 2021年3月-第02阶段-前端基础-Flex 伸缩布局-移动WEB开发_流式布局

    移动web开发流式布局 1.0 移动端基础 1.1 浏览器现状 PC端常见浏览器:360浏览器.谷歌浏览器.火狐浏览器.QQ浏览器.百度浏览器.搜狗浏览器.IE浏览器. 移动端常见浏览器:UC浏览器, ...

随机推荐

  1. 【druid 】数据库连接池-sql解析

    https://segmentfault.com/a/1190000008120254?utm_source=tuicool&utm_medium=referral sql解析 Druid 的 ...

  2. springboot 添加 jsp支持

    idea 新建springboot项目 packageing:选择 war next   勾选web 添加jar包 <dependency> <groupId>org.apac ...

  3. k8s之Hello World(四)

    实例:Guestbook留言板系统将通过Pod.RC.Service等资源对象搭建完成,成功启动后在网页中显示一条“Hello World”留言.其系统架构是一个基于PHP+Redis的分布式Web应 ...

  4. python selenium-webdriver 执行js (八)

    使用python selenium 做自动化测试的时候其实我基本没有使用过执行js,只是在后来使用selenium来爬虫的时候使用到了,所以我在这里也就拿出来简单的介绍一下如何执行js. execut ...

  5. delphi 多线程之 TEvent 和 TLightweightEvent

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  6. 使用xheditor时 cloneRange错误 ext.net

    使用ext.net 加  xheditor时,一直报 cloneRange错误. 于是 按照说明但独使用xheditor  ,检查无错,正常使用, 因此排除版本问题. <ext:panel ru ...

  7. C语言volatile关键字的用法

    volatile关键字的意义在于让被关键字修饰的变量每次使用时都重新去主内存里读取变量,而不是从高速缓存去读取. int a = 1; 普通变量定义之后,若要使用它,都会 先把它的值从主内存拷贝到高速 ...

  8. vs2017 F5不会自动编译了

    其实我的错误提示是:c# 不会命中断点 源代码与原始版本不同 我在网上搜索这个,发现让另存啦.格式化代码啦.批量重生成啦. 只有批量重生成好用,断点能打上,其他方法都不行. 可是每次都批量重生成太麻烦 ...

  9. AngularJS 1.x 思维索引

    我们在这里不讨论Angular2和Angular4,因为其完全重写,其实已经不叫AngularJS了. AngularJS的缺陷: 性能问题:通过检查脏值进行数据更新,当数据不断增加时,检查的效率就不 ...

  10. RAMOS和SSD对比

    http://bbs.pcbeta.com/forum.php?mod=redirect&goto=findpost&ptid=1786284&pid=48341400RAMO ...