This article is a mirror article of machine translation, please click here to jump to the original article.

View: 38588|Reply: 6

[.NET Core] ASP.NET Core (fourteen) is based on the SkiaSharp image captcha

[Copy link]
Posted on 2022-4-4 12:25:53 | | | |
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:

asp.net mvc beautiful image captcha
https://www.itsvse.com/thread-4911-1-1.html

WebAPI implements graphical verification code function
https://www.itsvse.com/thread-7304-1-1.html

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:

.NET Core watermarking issues on Linux pictures
https://www.itsvse.com/thread-9605-1-1.html

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, pleaseReply


(End)




Previous:[Turn]. NET 6 New Feature Enumerable.Chunk()
Next:Angular prevents the triggering of the parent click event
 Landlord| Posted on 2022-4-4 12:28:35 |
review

ASP.NET Core (XIII) to determine whether it is an Ajax request or not
https://www.itsvse.com/thread-10284-1-1.html

ASP.NET Core (twelve) front-end JS, CSS bundling, and compression
https://www.itsvse.com/thread-10282-1-1.html

ASP.NET Core (XI) endpoint route adds middleware to display all DI services
https://www.itsvse.com/thread-10269-1-1.html

ASP.NET Detailed explanation of Configuration priorities in Core(10).
https://www.itsvse.com/thread-10265-1-1.html

ASP.NET Detailed explanation of the Middleware middleware of Core (9).
https://www.itsvse.com/thread-9647-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
Posted on 2022-4-4 15:02:24 |
Learn to learn
Posted on 2022-4-13 08:58:42 |
This is good, I like it. Thanks for sharing!
Posted on 2022-4-14 09:46:41 |
Learn the operation of the big guy, thank you for sharing
Posted on 2022-8-5 16:25:05 |
The boss has worked hard
 Landlord| Posted on 2024-11-25 10:33:43 |
SixLabors.ImageSharp is also a great library for cross-platform image processing
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com