-
Notifications
You must be signed in to change notification settings - Fork 35
/
MainForm.cs
325 lines (288 loc) · 11.6 KB
/
MainForm.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using Lazy.Captcha.Core;
using Lazy.Captcha.Core.Generator;
using Lazy.Captcha.Core.Generator.Code;
using Lazy.Captcha.Core.Generator.Image.Option;
using Newtonsoft.Json;
using Sample.Winfrom.Helpers;
using Sample.Winfrom.Models;
using Sample.Winfrom.OptionProviders;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sample.Winfrom
{
public partial class MainForm : Form
{
private string captchaId = string.Empty;
private CaptchaService captchaService;
private CaptchaOptionsJsonModel captchaOptions;
private string tpl_File = Application.StartupPath + @"\templates\tpl.html";
private string config_File = Application.StartupPath + @"\templates\config.html";
private byte[] currentCaptchaBytes;
private Dictionary<string, PictureBox> fontPictureBoxMap;
public MainForm()
{
InitializeComponent();
InitFontPictureBoxMap();
BindDataSource();
GenerateCaptcha();
}
/// <summary>
/// 生成裁剪字体文字
/// </summary>
private void GeneateCropFontWords()
{
var words = new List<char>();
words.AddRange(Characters.CHINESE);
words.AddRange(Characters.NUMBER_ZH_CN);
words.AddRange(Characters.NUMBER_ZH_HK);
words.AddRange(Characters.WORD_LOWER);
words.AddRange(Characters.WORD_UPPER);
words.AddRange(Characters.WORD_UPPER);
words.AddRange(Characters.NUMBER);
words.Add('+');
words.Add('-');
words.Add('x');
words.Add('/');
words.Add('?');
words.Add('=');
words.Add('加');
words.Add('减');
words.Add('乘');
words.Add('除');
var text = string.Join("", words);
File.WriteAllText("D:/tmp/fonts/words.txt", text);
}
private void InitFontPictureBoxMap()
{
fontPictureBoxMap = new Dictionary<string, PictureBox>
{
{ "Actionj", this.Actionj_Pbx },
{ "Kaiti", this.Kaiti_Pbx },
{ "Fresnel", this.Fresnel_Pbx },
{ "Prefix", this.Prefix_Pbx },
{ "Ransom", this.Ransom_Pbx },
{ "Scandal", this.Scandal_Pbx },
{ "Epilog", this.Epilog_Pbx },
{ "Headache", this.Headache_Pbx },
{ "Lexo", this.Lexo_Pbx },
{ "Progbot", this.Progbot_Pbx },
{ "Robot", this.Robot_Pbx }
};
}
private void BindDataSource()
{
this.CpatchaType_Cbx.DataSource = CaptchaTypeOptionProvider.Provide();
this.FontFamily_Cbx.DataSource = FontFamilyOptionProvider.Provide();
}
private CaptchaOptionsJsonModel GenerateCaptchaOptions()
{
var fontFamilyOption = (FontFamilyOption)this.FontFamily_Cbx.SelectedItem;
return new CaptchaOptionsJsonModel()
{
CaptchaType = (int)this.CpatchaType_Cbx.SelectedValue,
CodeLength = (int)this.Length_Nud.Value,
ExpirySeconds = 60,
IgnoreCase = true,
StoreageKeyPrefix = "",
ImageOption = new CaptchaImageGeneratorOptionJsonModel
{
Animation = this.Gif_Cbx.Checked,
FrameDelay = (int)this.FrameDelay_Ndp.Value,
FontSize = (int)this.FontSize_Nud.Value,
Width = (int)this.Width_Nud.Value,
Height = (int)this.Height_Nud.Value,
BubbleMinRadius = (int)this.BubbleMinRadius_Nud.Value,
BubbleMaxRadius = (int)this.BubbleMaxRadius_Nud.Value,
BubbleCount = (int)this.BubbleCount_Nud.Value,
BubbleThickness = (int)this.BubbleThickness_Nud.Value,
InterferenceLineCount = (int)this.InterferenceLineCount_Nud.Value,
FontFamily = fontFamilyOption == null ? "Actionj" : fontFamilyOption.Text,
Quality = (int)this.Quality_Nud.Value,
TextBold = TextBold_Cbx.Checked
}
};
}
private CaptchaService GenerateCaptchaService(CaptchaOptionsJsonModel options)
{
var fontFamily = FontFamilyOptionProvider.Provide().First(e => e.Text == options.ImageOption.FontFamily).Value;
return CaptchaServiceBuilder
.New()
.CodeLength(options.CodeLength)
.CaptchaType((CaptchaType)options.CaptchaType)
.FontFamily(fontFamily)
.FontSize(options.ImageOption.FontSize)
.BubbleCount(options.ImageOption.BubbleCount)
.BubbleThickness(options.ImageOption.BubbleThickness)
.BubbleMinRadius(options.ImageOption.BubbleMinRadius)
.BubbleMaxRadius(options.ImageOption.BubbleMaxRadius)
.InterferenceLineCount(options.ImageOption.InterferenceLineCount)
.Animation(options.ImageOption.Animation)
.FrameDelay(options.ImageOption.FrameDelay)
.Width(options.ImageOption.Width)
.Height(options.ImageOption.Height)
.Quality(options.ImageOption.Quality)
.TextBold(options.ImageOption.TextBold)
.Build();
}
private CaptchaService GenerateCaptchaService()
{
this.captchaOptions = GenerateCaptchaOptions();
return GenerateCaptchaService(this.captchaOptions);
}
private void GenerateConfig()
{
// 生成配置
var wrapper = new CaptchaOptionsWrapper { CaptchaOptions = this.captchaOptions };
var json = JsonConvert.SerializeObject(wrapper, Formatting.Indented);
var jsonHtml = File.ReadAllText(tpl_File);
jsonHtml = jsonHtml.Replace("{{data}}", json);
File.WriteAllText(config_File, jsonHtml);
// 加载页面
WebBrowser.ScriptErrorsSuppressed = false; //禁用错误脚本提示
WebBrowser.IsWebBrowserContextMenuEnabled = true; // 禁用右键菜单
WebBrowser.WebBrowserShortcutsEnabled = true; //禁用快捷键
WebBrowser.AllowWebBrowserDrop = false; // 禁止文件拖动
WebBrowser.Navigate(config_File);
}
private void RenderCaptcha()
{
// 设定宽高
this.CaptchaPbx.Width = this.captchaOptions.ImageOption.Width;
this.CaptchaPbx.Height = this.captchaOptions.ImageOption.Height;
// 第一次比较慢,之后会很快
captchaId = Guid.NewGuid().ToString();
var data = captchaService.Generate(captchaId, 10);
CaptchaPbx.Image = Image.FromStream(new MemoryStream(data.Bytes));
currentCaptchaBytes = data.Bytes;
Code_Lbl.Text = data.Code;
// 生成全字体
foreach (var fontFamily in FontFamilyOptionProvider.Provide())
{
var option = this.GenerateCaptchaOptions();
option.ImageOption.Width = 98;
option.ImageOption.Height = 35;
option.ImageOption.FontFamily = fontFamily.Text;
var service = this.GenerateCaptchaService(option);
var id = Guid.NewGuid().ToString();
data = service.Generate(id, 10);
var pictureBox = this.fontPictureBoxMap[fontFamily.Text];
pictureBox.Image = Image.FromStream(new MemoryStream(data.Bytes));
}
}
/// <summary>
/// 生成验证码图
/// </summary>
private void GenerateCaptcha()
{
this.captchaService = GenerateCaptchaService();
this.RenderCaptcha();
this.GenerateConfig();
}
private void CaptchaPbx_Click(object sender, EventArgs e)
{
GenerateCaptcha();
}
private void CpatchaType_Cbx_SelectedIndexChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void FontFamily_Cbx_SelectedIndexChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void FontSize_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void Length_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void BubbleCount_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void BubbleThickness_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void BubbleMinRadius_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void BubbleMaxRadius_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void InterferenceLineCount_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void Gif_Cbx_CheckedChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
this.FrameDelay_Ndp.Enabled = this.Gif_Cbx.Checked;
}
private void FrameDelay_Ndp_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void Width_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void Height_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void Quality_Nud_ValueChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void TextBold_Cbx_CheckedChanged(object sender, EventArgs e)
{
this.GenerateCaptcha();
}
private void FetchSize_Btn_Click(object sender, EventArgs e)
{
var bytesCount = currentCaptchaBytes.Count();
var size = (bytesCount / 1000.0);
MessageBox.Show($"{size}kb");
}
private void Test_Btn_Click(object sender, EventArgs e)
{
this.Test_Btn.Enabled = false;
var test = new PerformanceTest(this.captchaOptions, 1000);
test.Complete += Test_Complete;
test.Progress += Test_Progress;
test.Start();
}
private void Test_Progress(int obj)
{
UIHelper.Invoke(this, () =>
{
this.Progress_Lbl.Text = $"%{obj}";
});
}
private void Test_Complete(int loopCount, long elapsedMilliseconds, long dataSize)
{
var perConsum = Math.Round(elapsedMilliseconds * 1.0 / loopCount, 1);
var oneSecondsCount = (int)(1000 / perConsum);
UIHelper.Invoke(this, () =>
{
this.Test_Btn.Enabled = true;
});
UIHelper.ShowMessageBox(this, $" 总计生成{loopCount}个\r\n 总计耗时{elapsedMilliseconds}毫秒\r\n 平均每个耗时{perConsum}毫秒\r\n 每秒可生成{oneSecondsCount}个\r\n 图片总计大小{UnitHelper.FormatFileSize(dataSize)}");
}
}
}