Skip to content

Commit

Permalink
add DrawLines option
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiWang committed Dec 15, 2023
1 parent f8adcee commit fd88c90
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Edi.Captcha.SampleApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void ConfigureServices(IServiceCollection services)
option.FontStyle = FontStyle.Bold;
//option.FontName = "Arial";
option.CodeLength = 4;
//option.DrawLines = false;
});
}

Expand Down
22 changes: 13 additions & 9 deletions src/Edi.Captcha/CaptchaImageGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace Edi.Captcha;

public static class CaptchaImageGenerator
{
public static CaptchaResult GetImage(int width, int height, string captchaCode, string fontName, FontStyle fontStyle = FontStyle.Regular)
public static CaptchaResult GetImage(
int width, int height, string captchaCode, string fontName, FontStyle fontStyle = FontStyle.Regular, bool drawLines = true)
{
using var ms = new MemoryStream();
var rand = new Random();
Expand All @@ -27,7 +28,7 @@ public static CaptchaResult GetImage(int width, int height, string captchaCode,
Font font = SystemFonts.CreateFont(fontName, fontSize, fontStyle);

Random random = new Random();

foreach (char c in captchaCode)
{
var x = rand.Next(5, 10);
Expand All @@ -36,7 +37,7 @@ public static CaptchaResult GetImage(int width, int height, string captchaCode,
var degrees = random.Next(0, 10) * (random.Next(-10, 10) > 0 ? 1 : -1);

var location = new PointF(x + position, y);
imgText.Mutate(ctx =>
imgText.Mutate(ctx =>
ctx.SetDrawingTransform(Matrix3x2Extensions.CreateRotationDegrees(degrees, new(0, 0)))
.DrawText(c.ToString(), font, GetRandomDeepColor(), location));
position += TextMeasurer.MeasureBounds(c.ToString(), new(font)).Width;
Expand All @@ -51,13 +52,16 @@ public static CaptchaResult GetImage(int width, int height, string captchaCode,
var img = new Image<Rgba32>(width, height);
img.Mutate(ctx => ctx.BackgroundColor(backColor));

// lines
for (var i = 0; i < rand.Next(3, 7); i++)
if (drawLines)
{
var color = GetRandomDeepColor();
var startPoint = new PointF(rand.Next(0, width), rand.Next(0, height));
var endPoint = new PointF(rand.Next(0, width), rand.Next(0, height));
img.Mutate(ctx => ctx.DrawLine(color, 1, startPoint, endPoint));
// lines
for (var i = 0; i < rand.Next(3, 7); i++)
{
var color = GetRandomDeepColor();
var startPoint = new PointF(rand.Next(0, width), rand.Next(0, height));
var endPoint = new PointF(rand.Next(0, width), rand.Next(0, height));
img.Mutate(ctx => ctx.DrawLine(color, 1, startPoint, endPoint));
}
}

// merge layers
Expand Down
2 changes: 1 addition & 1 deletion src/Edi.Captcha/Edi.Captcha.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>3.20.0</Version>
<Version>3.21.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company>edi.wang</Company>
<Authors>Edi Wang</Authors>
Expand Down
12 changes: 4 additions & 8 deletions src/Edi.Captcha/SessionBasedCaptcha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ namespace Edi.Captcha;
public class SessionBasedCaptchaOptions
{
public string SessionName { get; set; }
public FontStyle FontStyle { get; set; }
public FontStyle FontStyle { get; set; } = FontStyle.Regular;
public string FontName { get; set; }

public SessionBasedCaptchaOptions()
{
FontStyle = FontStyle.Regular;
}
public bool DrawLines { get; set; } = true;
}

public abstract class SessionBasedCaptcha : ISessionBasedCaptcha
Expand All @@ -29,7 +25,7 @@ public byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, i
EnsureHttpSession(httpSession);

var captchaCode = GenerateCaptchaCode();
var result = CaptchaImageGenerator.GetImage(width, height, captchaCode, Options.FontName, Options.FontStyle);
var result = CaptchaImageGenerator.GetImage(width, height, captchaCode, Options.FontName, Options.FontStyle, Options.DrawLines);
httpSession.SetString(sessionKeyName ?? Options.SessionName, result.CaptchaCode);
return result.CaptchaByteData;
}
Expand All @@ -39,7 +35,7 @@ public FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int
EnsureHttpSession(httpSession);

var captchaCode = GenerateCaptchaCode();
var result = CaptchaImageGenerator.GetImage(width, height, captchaCode, Options.FontName, Options.FontStyle);
var result = CaptchaImageGenerator.GetImage(width, height, captchaCode, Options.FontName, Options.FontStyle, Options.DrawLines);
httpSession.SetString(sessionKeyName ?? Options.SessionName, result.CaptchaCode);
Stream s = new MemoryStream(result.CaptchaByteData);
return new(s, "image/png");
Expand Down

0 comments on commit fd88c90

Please sign in to comment.