C# 验证码

C# 验证码

大家好,又见面了,我是全栈君。

作者:陈太汉

C# 验证码

多功能注册码,注册码可以完全自定义,全部都是动态的,包括字体的颜色,大小,样式,还有内容

 

复制代码
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");
}
}
复制代码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107873.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • win10环境下不通过cppan编译tesseract4.1动态库[通俗易懂]

    win10环境下不通过cppan编译tesseract4.1动态库[通俗易懂]在我的博客中,写了几篇关于tesseract如何编译Windows环境下的dll库,但那几篇都是基于cppan的环境依赖,而cppan的官方网站在2021年1月已经正式关闭了,因此那种方式将无法下载到对应的依赖环境,根据tesseract官方文档是使用SW的方式进行依赖下载,因此这篇博客将讲解使用基于SW的编译方式。1SW的环境配置在SW官方地址上下载sw.exe。将下载的sw.exe所在路径添加到系统环境变量PATH下。以管理员权限打开cmd,输入命令:swsetup这个命令主要是在用

    2022年9月30日
    2
  • 怎样创建一个简单的mysql数据库文件_MySQL数据库

    怎样创建一个简单的mysql数据库文件_MySQL数据库学习java到数据库操作章节后发现没有数据库,折腾了1天总算弄好了学习所需要的数据库,感觉好开心。一.创建数据库注:已经安装好mysql。windows下运行cmd进入命令窗口,本人用的是win7系统,先输入F:进入F盘,然后输入“cdF:\mysql\mysql-5.7.18-winx64\bin”(注:不要引号,路径为自己解压mysql的路径)。输入nets

    2025年7月2日
    3
  • 如何证明拉格朗日中值定理的正确性(泰勒公式秒杀高考压轴题)

    欢迎点击「算法与编程之美」↑关注我们!本文首发于微信公众号:”算法与编程之美”,欢迎关注,及时了解更多此系列文章。1问题描述很多人不明白怎样用罗尔定理来证明拉格朗日中值…

    2022年4月18日
    334
  • openGL研究钞四 : 关于颜色, 尺寸, 虚线, 多边形逆转, 空洞, 使用位图

    openGL研究钞四 : 关于颜色, 尺寸, 虚线, 多边形逆转, 空洞, 使用位图

    2022年1月14日
    56
  • python官网下载步骤图解-如何下载安装python 看完你就知道了[通俗易懂]

    python官网下载步骤图解-如何下载安装python 看完你就知道了[通俗易懂]工具/材料windows系统电脑操作方法01首先,在python的官网下载python版本,需要下载你电脑对应的版本,在【计算机】-【属性】中查看自己是32位还是64位操作系统(官网地址在网上自行搜索)。0232位和64位的版本安装起来没有区别,双击打开后,第一步要记得勾上AddpythontoPath选项,意思是把Python的安装路径添加到系统环境变量的Path变量中。然后选择自定义安…

    2022年4月30日
    125
  • pycharm的scrapy框架-断点调试「建议收藏」

    pycharm的scrapy框架-断点调试「建议收藏」在文件根目录,也就是settings.py的上级目录,scrapy.cfg的同级目录,创建main.py:fromscrapy.cmdlineimportexecuteimportosimportsysif__name__==’__main__’:sys.path.append(os.path.dirname(os.path.abspath(__file__)))execute([‘scrapy’,’crawl’,’你的spider的name’])点

    2022年5月11日
    44

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号