-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init * tweaks
- Loading branch information
Showing
12 changed files
with
82 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,99 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Drawing.Imaging; | ||
using System.IO; | ||
using System.Linq; | ||
using SixLabors.Fonts; | ||
using SixLabors.ImageSharp; | ||
using SixLabors.ImageSharp.Drawing.Processing; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
using SixLabors.ImageSharp.Processing; | ||
|
||
namespace CodeIndex.Server | ||
{ | ||
public class CaptchaImageUtils | ||
{ | ||
public static byte[] GenerateCaptchaImage(int width, int height, string captchaCode, Random random) | ||
{ | ||
using var baseMap = new Bitmap(width, height); | ||
using var graphics = Graphics.FromImage(baseMap); | ||
graphics.Clear(GetRandomLightColor(random)); | ||
DrawCaptchaCode(width, height, captchaCode, random, graphics); | ||
DrawDisorderLine(random, width, height, graphics); | ||
AdjustRippleEffect(baseMap); | ||
var fontSize = GetFontSize(width, captchaCode.Length); | ||
var fondFamily = SystemFonts.Collection.Families.FirstOrDefault(u => u.Name == "Consolas"); | ||
fondFamily = fondFamily == default ? SystemFonts.Collection.Families.Last() : fondFamily; | ||
var font = SystemFonts.CreateFont(fondFamily.Name, fontSize); | ||
|
||
using var ms = new MemoryStream(); | ||
baseMap.Save(ms, ImageFormat.Png); | ||
using var image = new Image<Rgba32>(width, height, GetRandomLightColor(random)); | ||
DrawCaptchaCode(height, captchaCode, fontSize, font, random, image); | ||
DrawDisorderLine(width, height, image, random); | ||
|
||
using var ms = new MemoryStream(); | ||
image.SaveAsPng(ms); | ||
return ms.ToArray(); | ||
} | ||
|
||
static int GetFontSize(int imageWidth, int captchCodeCount) | ||
static void DrawCaptchaCode(int height, string captchaCode, int fontSize, Font font, Random random, Image<Rgba32> image) | ||
{ | ||
var averageSize = imageWidth / captchCodeCount; | ||
for (int i = 0; i < captchaCode.Length; i++) | ||
{ | ||
var shiftPx = fontSize / 6; | ||
var x = random.Next(-shiftPx, shiftPx) + random.Next(-shiftPx, shiftPx); | ||
if (x < 0 && i == 0) | ||
{ | ||
x = 0; | ||
} | ||
|
||
return Convert.ToInt32(averageSize); | ||
x += i * fontSize; | ||
|
||
var maxY = height - fontSize; | ||
if (maxY < 0) | ||
{ | ||
maxY = 0; | ||
} | ||
|
||
var y = random.Next(0, maxY); | ||
|
||
image.Mutate(operation => operation.DrawText(captchaCode[i].ToString(), font, GetRandomDeepColor(random), new PointF(x, y))); | ||
} | ||
} | ||
|
||
static Color GetRandomDeepColor(Random random) | ||
{ | ||
var redlow = 160; | ||
var greenLow = 100; | ||
var blueLow = 160; | ||
return Color.FromArgb(random.Next(redlow), random.Next(greenLow), random.Next(blueLow)); | ||
return Color.FromRgb((byte)random.Next(redlow), (byte)random.Next(greenLow), (byte)random.Next(blueLow)); | ||
} | ||
|
||
static Color GetRandomLightColor(Random random) | ||
{ | ||
var low = 200; | ||
var high = 255; | ||
const int low = 200; | ||
const int high = 255; | ||
|
||
var nRend = random.Next(high) % (high - low) + low; | ||
var nGreen = random.Next(high) % (high - low) + low; | ||
var nBlue = random.Next(high) % (high - low) + low; | ||
|
||
return Color.FromArgb(nRend, nGreen, nBlue); | ||
return Color.FromRgb((byte)nRend, (byte)nGreen, (byte)nBlue); | ||
} | ||
|
||
static void DrawCaptchaCode(int width, int height, string captchaCode, Random random, Graphics graphics) | ||
static int GetFontSize(int imageWidth, int captchCodeCount) | ||
{ | ||
SolidBrush fontBrush = new SolidBrush(Color.Black); | ||
var fontSize = GetFontSize(width, captchaCode.Length); | ||
var font = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Bold, GraphicsUnit.Pixel); | ||
for (int i = 0; i < captchaCode.Length; i++) | ||
{ | ||
fontBrush.Color = GetRandomDeepColor(random); | ||
|
||
int shiftPx = fontSize / 6; | ||
|
||
var x = i * fontSize + random.Next(-shiftPx, shiftPx) + random.Next(-shiftPx, shiftPx); | ||
var maxY = height - fontSize; | ||
if (maxY < 0) | ||
{ | ||
maxY = 0; | ||
} | ||
|
||
var y = random.Next(0, maxY); | ||
var averageSize = imageWidth / captchCodeCount; | ||
|
||
graphics.DrawString(captchaCode[i].ToString(), font, fontBrush, x, y); | ||
} | ||
return Convert.ToInt32(averageSize); | ||
} | ||
|
||
static void DrawDisorderLine(Random random, int width, int height, Graphics graphics) | ||
static void DrawDisorderLine(int width, int height, Image graphics, Random random) | ||
{ | ||
var linePen = new Pen(new SolidBrush(Color.Black), 3); | ||
for (int i = 0; i < random.Next(3, 5); i++) | ||
{ | ||
linePen.Color = GetRandomLightColor(random); | ||
|
||
var linePen = new SolidPen(new SolidBrush(GetRandomLightColor(random)), 3); | ||
var startPoint = new Point(random.Next(0, width), random.Next(0, height)); | ||
var endPoint = new Point(random.Next(0, width), random.Next(0, height)); | ||
graphics.DrawLine(linePen, startPoint, endPoint); | ||
graphics.Mutate(operation => operation.DrawLine(linePen, startPoint, endPoint)); | ||
|
||
//var bezierPoint1 = new Point(random.Next(0, width), random.Next(0, height)); | ||
//var bezierPoint2 = new Point(random.Next(0, width), random.Next(0, height)); | ||
//graphics.DrawBezier(linePen, startPoint, bezierPoint1, bezierPoint2, endPoint); | ||
//var bezierPoint3 = new Point(random.Next(0, width), random.Next(0, height)); | ||
//var bezierPoint4 = new Point(random.Next(0, width), random.Next(0, height)); | ||
//graphics.Mutate(operation => operation.DrawBeziers(linePen, bezierPoint1, bezierPoint2, bezierPoint3, bezierPoint4)); | ||
} | ||
} | ||
|
||
static void AdjustRippleEffect(Bitmap baseMap) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters