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");
}
}
复制代码
目录
相关文章
|
C# 开发工具
C#滑动拼图验证码实现笔记
C# 是一个现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,由 Ecma 和 ISO 核准认可的。突发奇想,动手开发一个C#滑动拼图验证码,下面是我开发过程的记录。
C#滑动拼图验证码实现笔记
|
前端开发 机器人 C#
C#登录验证码
相信大家都明白,大多数的网站为了不被恶意访问,往往需要设置验证码登录,避免机器人访问,而造成我服务器瘫痪,崩溃,下面就来看看实现的具体过程吧!
C#登录验证码
|
前端开发 C# 数据安全/隐私保护
C#验证码
验证码通常是为了区分用户是人还是计算机,也可以防止破解密码、刷票等恶意行为,而客户端上多数会用在关键操作上,比如购买、登录、注册等场景。现在验证码的种类样式也特别多,今天教大家如何用C#做出滑动拼图验证码吧~
C#验证码
|
前端开发 C# 开发工具
想用C# .Net生成行为验证码,还得看这篇文章
为了增强网站的安全性,我们在网站的登录模块或信息输入模块加入了验证码功能,那么在C# .Net中如何实现验证码呢?本文借助KgCaptcha实现了这个功能。
想用C# .Net生成行为验证码,还得看这篇文章
|
C# 数据安全/隐私保护
C#实现手机发送验证码
C#实现手机发送验证码
502 0
|
关系型数据库 C# 数据安全/隐私保护