Previously, when developing ASP.NET projects using the .NET Framework, they were all deployed on Windows systems, and they could not be cross-platform, so you can use System.Drawing to generate image verification codes, as follows:
Developing online with .NET Core projects, generating image captchas requires reference to the System.Drawing.Common library, but for nowSystem.Drawing.Common NuGet package is now grouped as:WindowsSpecific libraries。 When compiling for non-Windows operating systems, the Platform Analyzer warns at compile time. As shown in the following figure:
This calling site is accessible on all platforms. 'Font' is only supported on 'windows'.
Prior to .NET 6, using the System.Drawing.Common package did not generate any compile-time warnings and did not throw any runtime exceptions. Starting with .NET 6, the Platform Analyzer issues a compile-time warning when compiling reference code for non-Windows operating systems. Also, unless the configuration option is set, the following runtime exception will be thrown:
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception. ---- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms. SeeThe hyperlink login is visible.for more information. Stack Trace: at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(String filename, IntPtr& bitmap) /_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(42,0): at System.Drawing.Bitmap.. ctor(String filename, Boolean useIcm) /_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(25,0): at System.Drawing.Bitmap.. ctor(String filename) /_/src/libraries/System.Resources.ResourceManager/tests/ResourceManagerTests.cs(270,0): at System.Resources.Tests.ResourceManagerTests.EnglishImageResourceData()+ MoveNext() /_/src/libraries/System.Linq/src/System/Linq/Select.cs(136,0): at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext() ----- Inner Stack Trace ----- /_/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs(31,0): at System.Drawing.LibraryResolver.EnsureRegistered() /_/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs(65,0): at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize() /_/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs(27,0): at System.Drawing.SafeNativeMethods.Gdip.. cctor() Error Reference:
For cross-platform image processing, Microsoft's official documentation recommends using the following libraries:
- ImageSharp
- SkiaSharp
- Microsoft.Maui.Graphics
Reference:The hyperlink login is visible.
This article uses the SkiaSharp library to generate graphical CAPTCHA,SkiaSharp is a cross-platform 2D graphics API for the .NET platform based on Google's Skia graphics library (skia.org).。 It offers a comprehensive 2D API that can be used across mobile, server, and desktop models to render images.
Address:The hyperlink login is visible.
First, create a new ASP.NET Core 6 project and referenceSkiaSharpThe relevant libraries are as follows:
Note that if the project needs to be deployed to a Linux system, references are requiredSkiaSharp.NativeAssets.Linuxclass, otherwise the error will be as follows:
Apr 03 18:51:06 raspberrypi dotnet[6203]: fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1] Apr 03 18:51:06 raspberrypi dotnet[6203]: An unhandled exception has occurred while executing the request. Apr 03 18:51:06 raspberrypi dotnet[6203]: System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception. Apr 03 18:51:06 raspberrypi dotnet[6203]: ---> System.DllNotFoundException: Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibSkiaSharp: cannot open shared object file: No such file or directory Apr 03 18:51:06 raspberrypi dotnet[6203]: at SkiaSharp.SkiaApi.sk_colortype_get_default_8888() Apr 03 18:51:06 raspberrypi dotnet[6203]: at SkiaSharp.SKImageInfo:. cctor() Apr 03 18:51:06 raspberrypi dotnet[6203]: --- End of inner exception stack trace --- Apr 03 18:51:06 raspberrypi dotnet[6203]: at HomeCloud.ImageCaptcha.GetCaptcha(String captchaText, Int32 width, Int32 height, Int32 lineNum, Int32 lineStrookeWidth) in C: \Users\itsvse_pc\source\repos\ConsoleApp1\HomeCloud\ImageCaptcha.cs:line 184 Apr 03 18:51:06 raspberrypi dotnet[6203]: at HomeCloud.Controllers.HomeController.Test() in C:\Users\itsvse_pc\source\repos\ConsoleApp1\HomeCloud\Controllers\HomeController.cs :line 80 Apr 03 18:51:06 raspberrypi dotnet[6203]: at lambda_method34(Closure , Object , Object[] ) In order to consider that some font fonts may be missing under different systems, resulting in the inability to display verification code information normally, we need to manually copy the fonts into the project, create a new fonts folder in the project, and copy the fonts into it, as shown in the figure below:
The configuration is as follows:
According to the reference materials on the Internet, the graphical verification code generated by the test is as follows:
The generated CAPTCHA has two disadvantages:The CAPTCHA is all black (easily recognized by OCR), and the CAPTCHA letters are crowded together without any spacingTo solve these two problems, I optimized the code as follows:
The HomeController controller code is as follows:
The final renderings are as follows:
Finally, attach the font file:
Tourists, if you want to see the hidden content of this post, please Reply
(End)
|