.net6之後,微軟默認不支持System.Drawing在linux上的使用,原有的方式在.net 6上需要配置發佈生成的*.runtimeconfig.json的configProperties節點下配置如下內容才支持,但在後續版本中刪除該開關。
1.依賴的程序集不要直接使用SkiaSharp,而要使用SkiaSharp.NativeAssets.Linux.NoDependencies
2.如上如果docker打包直接用官方的鏡像,那可能顯示驗證碼底圖可以顯示,但無法具體的驗證碼,字體什麼的加上也沒用的話,在Dockerfile裏還是需要添加libgdiplus的安裝引用(雖然微軟不推薦用這個,但安裝了它確實能顯示了,後續版本也可以繼續升級)
Dockerfile文件中添加
RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
附錄驗證碼生成代碼
1 public static byte[] GenerateValidateCode(string code, int width, int height, int textSize) 2 { 3 Random random = new(); 4 //驗證碼顏色集合 5 var colors = new[] { SKColors.Black, SKColors.Red, SKColors.DarkBlue, SKColors.Green, SKColors.Orange, SKColors.Brown, SKColors.DarkCyan, SKColors.Purple }; 6 var backcolors = new[] { SKColors.AntiqueWhite, SKColors.WhiteSmoke, SKColors.FloralWhite }; 7 //驗證碼字體集合 8 var fonts = new[] {"Cantarell" }; 9 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 10 { 11 fonts = new[] { "宋體" }; 12 } 13 //相當於js的 canvas.getContext('2d') 14 using var image2d = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Premul); 15 //相當於前端的canvas 16 using var canvas = new SKCanvas(image2d); 17 //填充白色背景 18 canvas.DrawColor(backcolors[random.Next(0, backcolors.Length - 1)]); 19 //樣式 跟xaml差不多 20 using var drawStyle = new SKPaint(); 21 //填充驗證碼到圖片 22 for (int i = 0; i < code.Length; i++) 23 { 24 drawStyle.IsAntialias = true; 25 drawStyle.TextSize = textSize; 26 var font = SKTypeface.FromFamilyName(fonts[random.Next(0, fonts.Length - 1)], SKFontStyleWeight.SemiBold, SKFontStyleWidth.ExtraCondensed, SKFontStyleSlant.Upright); 27 drawStyle.Typeface = font; 28 drawStyle.Color = colors[random.Next(0, colors.Length - 1)]; 29 //寫字 30 canvas.DrawText(code[i].ToString(), (i + 1) * 16, 28, drawStyle); 31 } 32 //生成6條幹擾線 33 for (int i = 0; i < 6; i++) 34 { 35 drawStyle.Color = colors[random.Next(colors.Length)]; 36 drawStyle.StrokeWidth = 1; 37 canvas.DrawLine(random.Next(0, code.Length * 15), random.Next(0, 60), random.Next(0, code.Length * 16), random.Next(0, 30), drawStyle); 38 } 39 //巴拉巴拉的就行了 40 using var img = SKImage.FromBitmap(image2d); 41 using var p = img.Encode(SKEncodedImageFormat.Png, 100); 42 using var ms = new MemoryStream(); 43 //保存到流 44 p.SaveTo(ms); 45 46 byte[] arr = new byte[ms.Length]; 47 ms.Position = 0; 48 ms.Read(arr, 0, (int)ms.Length); 49 return arr; 50 }
效果