1
- using System ;
1
+ using SkiaSharp ;
2
+ using System ;
2
3
using System . IO ;
3
4
using System . Linq ;
4
- using SkiaSharp ;
5
5
6
- namespace VOL . Core . Utilities ;
6
+ namespace VOL . Core . Utilities
7
+ {
8
+ public static class VierificationCodeHelpers
9
+ { //验证码字体集合
10
+ private static readonly string [ ] fonts = { "Verdana" , "Microsoft Sans Serif" , "Comic Sans MS" , "Arial" , "宋体" } ;
11
+ private static readonly SKColor [ ] colors = { SKColors . Black , SKColors . Green , SKColors . Brown } ;
7
12
8
- public static class VierificationCodeHelpers
9
- { //验证码字体集合
10
- private static readonly string [ ] fonts = { "Verdana" , "Microsoft Sans Serif" , "Comic Sans MS" , "Arial" , "宋体" } ;
11
- private static readonly SKColor [ ] colors = { SKColors . Black , SKColors . Red , SKColors . DarkBlue , SKColors . Green ,
12
- SKColors . Orange , SKColors . Brown , SKColors . DarkCyan , SKColors . Purple } ;
13
-
14
- /// <summary>
15
- ///
16
- /// </summary>
17
- /// <param name="code"></param>
18
- /// <returns></returns>
19
- public static string CreateBase64Image ( string code )
20
- {
21
- var random = new Random ( ) ;
22
- var info = new SKImageInfo ( ( int ) code . Length * 18 , 32 ) ;
23
- using var bitmap = new SKBitmap ( info ) ;
24
- using var canvas = new SKCanvas ( bitmap ) ;
13
+ /// <summary>
14
+ ///
15
+ /// </summary>
16
+ /// <param name="code"></param>
17
+ /// <returns></returns>
18
+ public static string CreateBase64Image ( string code )
19
+ {
20
+ var random = new Random ( ) ;
21
+ var info = new SKImageInfo ( ( int ) code . Length * 18 , 32 ) ;
22
+ using var bitmap = new SKBitmap ( info ) ;
23
+ using var canvas = new SKCanvas ( bitmap ) ;
25
24
26
- canvas . Clear ( SKColors . White ) ;
25
+ canvas . Clear ( SKColors . White ) ;
27
26
28
- using var pen = new SKPaint ( ) ;
29
- pen . FakeBoldText = true ;
30
- pen . Style = SKPaintStyle . Stroke ;
31
- pen . TextSize = 0.9f * info . Width * pen . TextSize / pen . MeasureText ( code ) ;
27
+ using var pen = new SKPaint ( ) ;
28
+ pen . FakeBoldText = true ;
29
+ pen . Style = SKPaintStyle . Fill ;
30
+ pen . TextSize = 20 ; // 0.6f * info.Width * pen.TextSize / pen.MeasureText(code);
32
31
33
- //绘制随机字符
34
- for ( int i = 0 ; i < code . Length ; i ++ )
35
- {
36
- pen . Color = random . GetRandom ( colors ) ; //随机颜色索引值
37
- pen . Typeface = SKTypeface . FromFamilyName ( random . GetRandom ( fonts ) , 700 , 20 , SKFontStyleSlant . Italic ) ; //配置字体
38
- var point = new SKPoint ( )
32
+ //绘制随机字符
33
+ for ( int i = 0 ; i < code . Length ; i ++ )
39
34
{
40
- X = i * 12 ,
41
- Y = info . Height - ( ( i + 1 ) % 2 == 0 ? 2 : 4 )
42
- } ;
43
- canvas . DrawText ( code . Substring ( i , 1 ) , point , pen ) ; //绘制一个验证字符
44
- }
35
+ pen . Color = random . GetRandom ( colors ) ; //随机颜色索引值
36
+ pen . Typeface = SKTypeface . FromFamilyName ( random . GetRandom ( fonts ) , 700 , 20 , SKFontStyleSlant . Italic ) ; //配置字体
37
+ var point = new SKPoint ( )
38
+ {
39
+ X = i * 16 ,
40
+ Y = 22 // info.Height - ((i + 1) % 2 == 0 ? 2 : 4),
45
41
46
- //绘制噪点
47
- var points = Enumerable . Range ( 0 , 100 ) . Select (
48
- _ => new SKPoint ( random . Next ( bitmap . Width ) , random . Next ( bitmap . Height ) )
49
- ) . ToArray ( ) ;
50
- canvas . DrawPoints (
51
- SKPointMode . Points ,
52
- points ,
53
- pen ) ;
42
+ } ;
43
+ canvas . DrawText ( code . Substring ( i , 1 ) , point , pen ) ; //绘制一个验证字符
44
+ }
54
45
55
- //绘制贝塞尔线条
56
- for ( int i = 0 ; i < 2 ; i ++ )
57
- {
58
- var p1 = new SKPoint ( 0 , random . Next ( bitmap . Height ) ) ;
59
- var p2 = new SKPoint ( random . Next ( bitmap . Width ) , random . Next ( bitmap . Height ) ) ;
60
- var p3 = new SKPoint ( random . Next ( bitmap . Width ) , random . Next ( bitmap . Height ) ) ;
61
- var p4 = new SKPoint ( bitmap . Width , random . Next ( bitmap . Height ) ) ;
46
+ //绘制噪点
47
+ var points = Enumerable . Range ( 0 , 100 ) . Select (
48
+ _ => new SKPoint ( random . Next ( bitmap . Width ) , random . Next ( bitmap . Height ) )
49
+ ) . ToArray ( ) ;
50
+ canvas . DrawPoints (
51
+ SKPointMode . Points ,
52
+ points ,
53
+ pen ) ;
54
+
55
+ //绘制贝塞尔线条
56
+ for ( int i = 0 ; i < 2 ; i ++ )
57
+ {
58
+ var p1 = new SKPoint ( 0 , 0 ) ;
59
+ var p2 = new SKPoint ( 0 , 0 ) ;
60
+ var p3 = new SKPoint ( 0 , 0 ) ;
61
+ var p4 = new SKPoint ( 0 , 0 ) ;
62
62
63
- var touchPoints = new SKPoint [ ] { p1 , p2 , p3 , p4 } ;
63
+ var touchPoints = new SKPoint [ ] { p1 , p2 , p3 , p4 } ;
64
64
65
- using var bPen = new SKPaint ( ) ;
66
- bPen . Color = random . GetRandom ( colors ) ;
67
- bPen . Style = SKPaintStyle . Stroke ;
65
+ using var bPen = new SKPaint ( ) ;
66
+ bPen . Color = random . GetRandom ( colors ) ;
67
+ bPen . Style = SKPaintStyle . Stroke ;
68
68
69
- using var path = new SKPath ( ) ;
70
- path . MoveTo ( touchPoints [ 0 ] ) ;
71
- path . CubicTo ( touchPoints [ 1 ] , touchPoints [ 2 ] , touchPoints [ 3 ] ) ;
72
- canvas . DrawPath ( path , bPen ) ;
69
+ using var path = new SKPath ( ) ;
70
+ path . MoveTo ( touchPoints [ 0 ] ) ;
71
+ path . CubicTo ( touchPoints [ 1 ] , touchPoints [ 2 ] , touchPoints [ 3 ] ) ;
72
+ canvas . DrawPath ( path , bPen ) ;
73
+ }
74
+ return bitmap . ToBase64String ( SKEncodedImageFormat . Png ) ;
73
75
}
74
- return bitmap . ToBase64String ( SKEncodedImageFormat . Png ) ;
75
- }
76
76
77
- public static T GetRandom < T > ( this Random random , T [ ] tArray )
78
- {
79
- if ( random == null ) random = new Random ( ) ;
80
- return tArray [ random . Next ( tArray . Length ) ] ;
81
- }
77
+ public static T GetRandom < T > ( this Random random , T [ ] tArray )
78
+ {
79
+ if ( random == null ) random = new Random ( ) ;
80
+ return tArray [ random . Next ( tArray . Length ) ] ;
81
+ }
82
82
83
- /// <summary>
84
- /// SKBitmap转Base64String
85
- /// </summary>
86
- /// <param name="bitmap"></param>
87
- /// <param name="format"></param>
88
- /// <returns></returns>
89
- public static string ToBase64String ( this SKBitmap bitmap , SKEncodedImageFormat format )
90
- {
91
- using var memStream = new MemoryStream ( ) ;
92
- using var wstream = new SKManagedWStream ( memStream ) ;
93
- bitmap . Encode ( wstream , format , 32 ) ;
94
- memStream . TryGetBuffer ( out ArraySegment < byte > buffer ) ;
95
- return $ "{ Convert . ToBase64String ( buffer . Array , 0 , ( int ) memStream . Length ) } ";
83
+ /// <summary>
84
+ /// SKBitmap转Base64String
85
+ /// </summary>
86
+ /// <param name="bitmap"></param>
87
+ /// <param name="format"></param>
88
+ /// <returns></returns>
89
+ public static string ToBase64String ( this SKBitmap bitmap , SKEncodedImageFormat format )
90
+ {
91
+ using var memStream = new MemoryStream ( ) ;
92
+ using var wstream = new SKManagedWStream ( memStream ) ;
93
+ bitmap . Encode ( wstream , format , 32 ) ;
94
+ memStream . TryGetBuffer ( out ArraySegment < byte > buffer ) ;
95
+ return $ "{ Convert . ToBase64String ( buffer . Array , 0 , ( int ) memStream . Length ) } ";
96
+ }
96
97
}
97
98
}
0 commit comments