Repository navigation
Expand file tree
/
Copy pathGeneration.cs
More file actions
252 lines (221 loc) · 10.9 KB
/
Copy pathGeneration.cs
File metadata and controls
252 lines (221 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#region Related components
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using SkiaSharp;
#endregion
namespace net.vieapps.Components.Utility
{
public static partial class ImageExtensions
{
/// <summary>
/// Generates image
/// </summary>
/// <param name="bytes"></param>
/// <param name="format"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="asBig"></param>
/// <param name="isWebP"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<byte[]> GenerateImageAsync(this byte[] bytes, string format, int width, int height, bool asBig, bool isWebP, CancellationToken cancellationToken = default)
{
if (width < 1 && height < 1)
return isWebP && "WebP".IsSameFormat(format) ? bytes : await bytes.ConvertImageAsync(format, cancellationToken).ConfigureAwait(false);
using var source = bytes.DecodeBitmap();
cancellationToken.ThrowIfCancellationRequested();
var size = ImageExtensions.GetFitSize(source.Width, source.Height, width, height);
if ("WebP".IsSameFormat(format) && ImageExtensions.ResizeBigWebpImage && size.Width > ImageExtensions.WebpMaxWidth)
size = ImageExtensions.GetFitSize(size.Width, size.Height, ImageExtensions.WebpMaxWidth, 0);
cancellationToken.ThrowIfCancellationRequested();
using var resized = size.Width == source.Width && size.Height == source.Height ? null : ImageExtensions.ResizeTo(source, size, asBig);
cancellationToken.ThrowIfCancellationRequested();
using var stream = await (resized ?? source).EncodeToStreamAsync(format, true, cancellationToken).ConfigureAwait(false);
return stream.ToBytes();
}
static SKBitmap GenerateBitmap(this Exception ex, int width, int height)
{
var bitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Opaque));
try
{
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(SKColors.White);
using var font = new SKFont(ImageExtensions.GetTypeface("Arial", true), 16);
using var paint = new SKPaint { IsAntialias = true, Color = SKColors.Black };
ImageExtensions.DrawTextAtTop(canvas, ex?.Message ?? "Unknown error", 20, 40, font, paint);
}
return bitmap;
}
catch
{
bitmap.Dispose();
throw;
}
}
/// <summary>
/// Generates image (exception)
/// </summary>
/// <param name="ex"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="format"></param>
/// <returns></returns>
public static byte[] GenerateImage(this Exception ex, int width, int height, string format = "WebP")
{
using var bitmap = ex.GenerateBitmap(width, height);
using var stream = bitmap.EncodeToStream(format ?? "WebP", true);
return stream.ToBytes();
}
/// <summary>
/// Generates image (exception)
/// </summary>
/// <param name="ex"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="format"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<byte[]> GenerateImageAsync(this Exception ex, int width, int height, string format = "WebP", CancellationToken cancellationToken = default)
{
using var bitmap = ex.GenerateBitmap(width, height);
using var stream = await bitmap.EncodeToStreamAsync(format ?? "WebP", true, cancellationToken).ConfigureAwait(false);
return stream.ToBytes();
}
static ConcurrentDictionary<string, SKTypeface> Typefaces { get; } = new(StringComparer.OrdinalIgnoreCase);
static SKTypeface GetTypeface(string family, bool bold)
=> ImageExtensions.Typefaces.GetOrAdd($"{family}|{bold}", _ => SKTypeface.FromFamilyName(family, bold ? SKFontStyle.Bold : SKFontStyle.Normal) ?? SKTypeface.Default);
static void DrawTextAtTop(SKCanvas canvas, string text, float x, float top, SKFont font, SKPaint paint)
=> canvas.DrawText(text, x, top - font.Metrics.Ascent, SKTextAlign.Left, font, paint);
static SKColor[] ToColors(params string[] hexes)
=> hexes.Select(SKColor.Parse).ToArray();
static SKColor[] SmallCaptchaBackgroundColors { get; } = ImageExtensions.ToColors("#FFA500", "#D8BFD8", "#20B2AA", "#FFFF00", "#9ACD32", "#FFDEAD", "#FFFFFF");
static SKColor[] BigCaptchaBackgroundColors { get; } = ImageExtensions.ToColors("#FFA500", "#D8BFD8", "#20B2AA", "#EE82EE", "#FFFF00", "#9ACD32", "#FFDEAD", "#D3D3D3", "#FF6347", "#90EE90", "#FFFFFF");
static SKColor[] CaptchaFontColors { get; } = ImageExtensions.ToColors("#000000", "#0000FF", "#00008B", "#006400", "#FF00FF", "#FF0000", "#8B0000", "#000000", "#B22222", "#006400", "#9400D3");
static string[] CaptchaFontNames { get; } = ["Verdana", "Arial", "Times New Roman", "Courier", "Courier New"];
static SKBitmap GenerateBitmapBackround(int width, int height, SKColor[] colors)
{
var width1 = UtilityService.GetRandomNumber(UtilityService.GetRandomNumber(5, 10), UtilityService.GetRandomNumber(20, width / 2));
var height1 = UtilityService.GetRandomNumber(UtilityService.GetRandomNumber(1, 10), UtilityService.GetRandomNumber(12, height));
var width2 = UtilityService.GetRandomNumber(UtilityService.GetRandomNumber(15, width / 3), UtilityService.GetRandomNumber(width / 3, width / 2));
var height2 = UtilityService.GetRandomNumber(UtilityService.GetRandomNumber(5, height / 4), UtilityService.GetRandomNumber(height / 4, height / 2));
var width3 = UtilityService.GetRandomNumber(UtilityService.GetRandomNumber(width / 4, width / 2), UtilityService.GetRandomNumber(width / 2, width));
var height3 = UtilityService.GetRandomNumber(UtilityService.GetRandomNumber(height / 5, height / 2), UtilityService.GetRandomNumber(height / 2, height));
var background = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Opaque));
try
{
using var canvas = new SKCanvas(background);
canvas.Clear(colors[3]);
using var paint = new SKPaint { Style = SKPaintStyle.Fill };
paint.Color = colors[0];
canvas.DrawRect(UtilityService.GetRandomNumber(0, width / 2), UtilityService.GetRandomNumber(0, height / 2), width1, height1, paint);
paint.Color = colors[1];
canvas.DrawRect(UtilityService.GetRandomNumber(width / 5, width / 2), UtilityService.GetRandomNumber(height / 5, height / 2), width2, height2, paint);
paint.Color = colors[2];
canvas.DrawRect(UtilityService.GetRandomNumber(width / 4, width / 3), UtilityService.GetRandomNumber(0, height / 3), width3, height3, paint);
return background;
}
catch
{
background.Dispose();
throw;
}
}
static SKBitmap GenerateBitmap(this string code, bool isSmall = true)
{
var width = isSmall ? 110 : 220;
var height = isSmall ? 24 : 48;
var backgroundColors = isSmall ? ImageExtensions.SmallCaptchaBackgroundColors : ImageExtensions.BigCaptchaBackgroundColors;
using var background = ImageExtensions.GenerateBitmapBackround(width, height,
[
backgroundColors[UtilityService.GetRandomNumber(0, backgroundColors.Length)],
backgroundColors[UtilityService.GetRandomNumber(0, backgroundColors.Length)],
backgroundColors[UtilityService.GetRandomNumber(0, backgroundColors.Length)],
backgroundColors[UtilityService.GetRandomNumber(0, backgroundColors.Length)]
]);
using (var canvas = new SKCanvas(background))
{
if (isSmall)
{
using var linePaint = new SKPaint { IsAntialias = true, Color = SKColors.Gray };
for (var index = 0; index < 6; index++)
{
linePaint.StrokeWidth = UtilityService.GetRandomNumber(1, 2);
canvas.DrawLine(UtilityService.GetRandomNumber(0, width), UtilityService.GetRandomNumber(0, height), UtilityService.GetRandomNumber(0, width), UtilityService.GetRandomNumber(0, height), linePaint);
}
}
using var textPaint = new SKPaint { IsAntialias = true };
var step = 0;
for (var index = 0; index < code.Length; index++)
{
using var font = new SKFont(ImageExtensions.GetTypeface(ImageExtensions.CaptchaFontNames[UtilityService.GetRandomNumber(0, ImageExtensions.CaptchaFontNames.Length)], true), isSmall ? 16 : 20);
var x = index * 7 + step + UtilityService.GetRandomNumber(-1, 9);
var y = UtilityService.GetRandomNumber(-2, 2);
textPaint.Color = ImageExtensions.CaptchaFontColors[UtilityService.GetRandomNumber(0, ImageExtensions.CaptchaFontColors.Length)];
var writtenCode = code.Substring(index, 1);
if (writtenCode.Equals("I") || (UtilityService.GetRandomNumber() % 2 == 1 && !writtenCode.Equals("L")))
writtenCode = writtenCode.ToLower();
ImageExtensions.DrawTextAtTop(canvas, writtenCode, x, y, font, textPaint);
step += UtilityService.GetRandomNumber(13, 23);
}
}
var noiseColor = SKColor.Parse("#A9A9A9");
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
if (x % 3 == 1 && y % 4 == 1)
background.SetPixel(x, y, noiseColor);
var divideTo = 64.0d + UtilityService.GetRandomNumber(1, 10);
var distortion = isSmall ? UtilityService.GetRandomNumber(1, 5) : UtilityService.GetRandomNumber(5, 11);
var distorted = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Opaque));
try
{
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
{
var newX = (int)(x + distortion * Math.Sin(Math.PI * y / divideTo));
var newY = (int)(y + distortion * Math.Cos(Math.PI * x / divideTo));
if (newX < 0 || newX >= width)
newX = 0;
if (newY < 0 || newY >= height)
newY = 0;
distorted.SetPixel(x, y, background.GetPixel(newX, newY));
}
return distorted;
}
catch
{
distorted.Dispose();
throw;
}
}
/// <summary>
/// Generates image (captcha)
/// </summary>
/// <param name="code"></param>
/// <param name="isSmall"></param>
/// <param name="format"></param>
/// <returns></returns>
public static byte[] GenerateImage(this string code, bool isSmall = true, string format = "WebP")
{
using var bitmap = code.GenerateBitmap(isSmall);
using var stream = bitmap.EncodeToStream(format, true);
return stream.ToBytes();
}
/// <summary>
/// Generates image (captcha)
/// </summary>
/// <param name="code"></param>
/// <param name="isSmall"></param>
/// <param name="format"></param>
/// <returns></returns>
public static async Task<byte[]> GenerateImageAsync(this string code, bool isSmall = true, string format = "WebP", CancellationToken cancellationToken = default)
{
using var bitmap = code.GenerateBitmap(isSmall);
using var stream = await bitmap.EncodeToStreamAsync(format, true, cancellationToken).ConfigureAwait(false);
return stream.ToBytes();
}
}
}