-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaptchaImageBuilder.cs
49 lines (44 loc) · 1.25 KB
/
CaptchaImageBuilder.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Captcha_K;
internal class CaptchaImageBuilder : IDisposable
{
public CaptchaImageBuilder(int height, int width)
{
bitmap = new Bitmap(width, height);
graphics = Graphics.FromImage(bitmap);
}
private readonly Graphics graphics;
private readonly Bitmap bitmap;
public CaptchaImageBuilder FillBackground(Color color)
{
graphics.Clear(color);
return this;
}
public CaptchaImageBuilder DrawText(string text, Color textColor, Font? font = null)
{
font ??= new Font("Consolas", bitmap.Height / 2, FontStyle.Bold);
graphics.DrawString(text, font, new SolidBrush(textColor), new PointF(0, 0));
return this;
}
public CaptchaImageBuilder AddEffect(Action<Graphics, int, int> effectAction)
{
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
effectAction.Invoke(graphics, x, y);
}
}
return this;
}
public Bitmap Image() => bitmap;
public void Dispose()
{
graphics.Dispose();
}
}