博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随机笔记
阅读量:5337 次
发布时间:2019-06-15

本文共 3967 字,大约阅读时间需要 13 分钟。

public interface IRandomTextGenerator

{
/// <summary>
/// Gets or sets the settings.
/// </summary>
/// <value>The settings.</value>
RandomTextGeneratorSettings Settings { get; set; }

/// <summary>
/// Generates this instance.
/// </summary>
/// <returns></returns>
string Generate();
}

/// <summary>

/// Settings for the random text generator.
/// </summary>
public class RandomTextGeneratorSettings
{
/// <summary>
/// Gets or sets the length of random charachters to generate
/// </summary>
/// <value>The length.</value>
public int Length { get; set; }

/// <summary>
/// Gets or sets the allowed chars.
/// </summary>
/// <value>The allowed chars.</value>
public string AllowedChars { get; set; }
}

 

 

 

 

 

public class RandomTextGenerator : IRandomTextGenerator

{

/// <summary>

/// Initializes a new instance of the <see cref="RandomTextGenerator"/> class.
/// </summary>
public RandomTextGenerator()
{
Settings = new RandomTextGeneratorSettings();
Settings.Length = 5;
Settings.AllowedChars = "ABCDEFGHJKLMNPRSTUVWXY3456789";
}

#region IRandomTextGenerator Members
/// <summary>
/// Gets or sets the settings.
/// </summary>
/// <value>The settings.</value>
public RandomTextGeneratorSettings Settings { get; set; }

/// <summary>
/// Generate the random text.
/// </summary>
/// <returns></returns>
public string Generate()
{
Byte[] randomBytes = new Byte[Settings.Length];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
char[] chars = new char[Settings.Length];
int allowedCharCount = Settings.AllowedChars.Length;
string allowedChars = Settings.AllowedChars;

for (int i = 0; i < Settings.Length; i++)

{
chars[i] = allowedChars[(int)randomBytes[i] % allowedCharCount];
}

return new string(chars);

}
#endregion
}

 

 

 

public class CaptchaImageActionResult : ActionResult

{
public string RandomWord { get; set; }

public override void ExecuteResult(ControllerContext context)

{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)Math.Ceiling((RandomWord.Length * 14.5)), 22);
Graphics graphics = Graphics.FromImage(bmp);

Random random = new Random();

graphics.Clear(Color.White);
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(bmp.Width);
int x2 = random.Next(bmp.Width);
int y1 = random.Next(bmp.Height);
int y2 = random.Next(bmp.Height);
if (i / 2 == 0)
{
graphics.DrawLine(new Pen(Color.DarkCyan), x1, y1, x2, y2);
}
else
{
graphics.DrawLine(new Pen(Color.Chocolate), x1, y1, x2, y2);
}
graphics.Flush();
}

Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bmp.Width, bmp.Height), Color.Blue, Color.DarkRed, 1.2f, true);
graphics.DrawString(RandomWord, font, brush, 2, 2);
for (int i = 0; i < 5; i++)
{
int x = random.Next(bmp.Width);
int y = random.Next(bmp.Height);

bmp.SetPixel(x, y, Color.FromArgb(random.Next()));

}
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);

context.HttpContext.Response.ContentType = "image/gif";

bmp.Save(context.HttpContext.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

font.Dispose();

graphics.Dispose();
bmp.Dispose();
}
}

 

 

public CaptchaImageActionResult CaptchaImage()

{
var setting = new Framework.Utility.Captcha.RandomTextGenerator();
setting.Settings.Length = 4;
string strRandom = setting.Generate();
HttpContext.Session["RandomText"] = strRandom;
return new CaptchaImageActionResult() { RandomWord = strRandom };
}

 

 

验证码

<input value="" type="text" name="CaptchaImage" id="CaptchaImage" style="width: 60px;" />
<img style="cursor:pointer; vertical-align:middle" title="看不清楚请刷新!" id="valiCode" style="cursor: pointer;" src="/login/CaptchaImage" alt="" οnclick="this.src='/login/CaptchaImage?dt='+ new Date().getTime()"/>
<span class="Types" style="color: Red">*</span>

转载于:https://www.cnblogs.com/miao-park/archive/2012/11/28/2793096.html

你可能感兴趣的文章
vue地址插件多级联动自适应 + github地址
查看>>
ODE 笔记
查看>>
你真的懂示波器吗?工作面试中会用到的示波器知识(转)
查看>>
9.Action类接收参数(原生的ServletAPI )
查看>>
OSI七层模型和tcp/ip四层模型对比
查看>>
Dockerfile指令介绍
查看>>
Docker 快速删除所有容器
查看>>
Linux命令学习手册-printf命令(转)
查看>>
理解Lock例子
查看>>
Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)
查看>>
Javascript禁止父元素滚动条滚动, pc、移动端均有效
查看>>
跳转网页
查看>>
Silverlight 4 安装出现语言版本错误
查看>>
Matlab——表达式 阵列与矩阵的创建
查看>>
匿名管道 双向通信 需要两个
查看>>
Android系统手机端抓包方法
查看>>
Vue的filter属性
查看>>
PHP CI 框架简单使用(二)
查看>>
RabbitMq
查看>>
POJ2155:Matrix
查看>>