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#实现手机发送验证码
436 0
|
JSON C# 数据安全/隐私保护
随手记_C#验证码
前言 最近在网上偶然看见一个验证码,觉得很有意思,于是搜了下,是使用第三方实现的,先看效果:     总体来说效果还是可以的,官方提供的SDK也比较详细,可配置性很高。在这里在简单啰嗦几句使用方式: 使用步骤 ①进入官网下载sdk接口→ http://www.geetest.com/install/ ,因为小弟是做C#的,所以此处选择C#,具体选择看各位大佬所用语言决定~ ②第二步,获取代码,访问红框所示地址,下载demo。
1152 0
|
C# 数据安全/隐私保护
C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
原文:C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站 我们经常会碰到需要程序模拟登录一个网站,那如果网站需要填写验证码的要怎样模拟登录呢?这篇文章利用了 HttpWebRequest 和 HttpWebResponse 模拟登录了有验证码的网站。
1341 0
|
Web App开发 .NET C#
ASP.NET with C#生成验证码的过程
ASP.NET with C#生成验证码的过程 生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。
887 0