From b2ad000e07e1c87d3561b5aa94fdc88c779872f0 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期二, 18 二月 2025 22:34:54 +0800
Subject: [PATCH] 1

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/VierificationCode.cs |  122 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 122 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/VierificationCode.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/VierificationCode.cs"
new file mode 100644
index 0000000..b0cdec4
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/VierificationCode.cs"
@@ -0,0 +1,122 @@
+锘縰sing SkiaSharp;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace WIDESEA_Core.Utilities
+{
+    public static class VierificationCode
+    {
+        private static readonly string[] _chars = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
+
+        private static readonly SKColor[] colors = { SKColors.Black, SKColors.Green, SKColors.Brown };
+
+        private static readonly string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "瀹嬩綋" };
+        public static string RandomText()
+        {
+            string code = "";//浜х敓鐨勯殢鏈烘暟
+            int temp = -1;
+            Random rand = new Random();
+            for (int i = 1; i < 5; i++)
+            {
+                if (temp != -1)
+                {
+                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
+                }
+                int t = rand.Next(61);
+                if (temp != -1 && temp == t)
+                {
+                    return RandomText();
+                }
+                temp = t;
+                code += _chars[t];
+            }
+            return code;
+        }
+        public static string CreateBase64Imgage(string code)
+        {
+            var random = new Random();
+            var info = new SKImageInfo((int)code.Length * 18, 32);
+            using var bitmap = new SKBitmap(info);
+            using var canvas = new SKCanvas(bitmap);
+
+            canvas.Clear(SKColors.White);
+
+            using var pen = new SKPaint();
+            pen.FakeBoldText = true;
+            pen.Style = SKPaintStyle.Fill;
+            pen.TextSize = 20;// 0.6f * info.Width * pen.TextSize / pen.MeasureText(code);
+
+            //缁樺埗闅忔満瀛楃
+            for (int i = 0; i < code.Length; i++)
+            {
+                pen.Color = random.GetRandom(colors);//闅忔満棰滆壊绱㈠紩鍊�
+
+                pen.Typeface = SKTypeface.FromFamilyName(random.GetRandom(fonts), 700, 20, SKFontStyleSlant.Italic);//閰嶇疆瀛椾綋
+                var point = new SKPoint()
+                {
+                    X = i * 16,
+                    Y = 22// info.Height - ((i + 1) % 2 == 0 ? 2 : 4),
+
+                };
+                canvas.DrawText(code.Substring(i, 1), point, pen);//缁樺埗涓�涓獙璇佸瓧绗�
+
+            }
+
+            // 缁樺埗鍣偣
+            var points = Enumerable.Range(0, 100).Select(
+                _ => new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height))
+            ).ToArray();
+            canvas.DrawPoints(
+                SKPointMode.Points,
+                points,
+                pen);
+
+            //缁樺埗璐濆灏旂嚎鏉�
+            for (int i = 0; i < 2; i++)
+            {
+                var p1 = new SKPoint(0, 0);
+                var p2 = new SKPoint(0, 0);
+                var p3 = new SKPoint(0, 0);
+                var p4 = new SKPoint(0, 0);
+
+                var touchPoints = new SKPoint[] { p1, p2, p3, p4 };
+
+                using var bPen = new SKPaint();
+                bPen.Color = random.GetRandom(colors);
+                bPen.Style = SKPaintStyle.Stroke;
+
+                using var path = new SKPath();
+                path.MoveTo(touchPoints[0]);
+                path.CubicTo(touchPoints[1], touchPoints[2], touchPoints[3]);
+                canvas.DrawPath(path, bPen);
+            }
+            return bitmap.ToBase64String(SKEncodedImageFormat.Png);
+        }
+
+        public static T GetRandom<T>(this Random random, T[] tArray)
+        {
+            if (random == null) random = new Random();
+            return tArray[random.Next(tArray.Length)];
+        }
+
+        /// <summary>
+        /// SKBitmap杞珺ase64String
+        /// </summary>
+        /// <param name="bitmap"></param>
+        /// <param name="format"></param>
+        /// <returns></returns>
+        public static string ToBase64String(this SKBitmap bitmap, SKEncodedImageFormat format)
+        {
+            using var memStream = new MemoryStream();
+            using var wstream = new SKManagedWStream(memStream);
+            bitmap.Encode(wstream, format, 32);
+            memStream.TryGetBuffer(out ArraySegment<byte> buffer);
+            return $"{Convert.ToBase64String(buffer.Array, 0, (int)memStream.Length)}";
+        }
+    }
+}

--
Gitblit v1.9.3