Sample codes for using SkiaSharp to implement verification codes for ASP. NET Core and skiasharp
Preface
This article does not implement a complete verification code sample, but provides another idea for using the Drawing API in. NET Core 2.0 and demonstrates it in the form of a simple Demo.
Skia
Skia is an open-source two-dimensional graphics library that provides a variety of common APIs and can be run on a variety of software and hardware platforms. Google Chrome, Chrome OS, Android, Firefox browser, Firefox operating system, and many other products use it as a graphics engine.
Skia is managed by Google, and anyone can use Skia Based on the BSD free software license. The Skia development team is committed to developing its Core Components and widely adopts the open source contributions made by all parties to Skia.
SkiaSharp
SkiaSharp is a cross-platform 2D graphics. net api binding launched by Mono Based on Google's Skia graphics library. Provides a comprehensive 2D API for graphic rendering and image processing across mobile, server, and desktop modes.
Skiasharp provides PCL and platform-specific binding:
- . NET Core/. NET Standard 1.3
- Xamarin. Android
- Xamarin. iOS
- Xamarin. tvOS
- Xamarin. Mac
- Windows Classic Desktop (Windows. Forms/WPF)
- Windows UWP (Desktop/Mobile/Xbox/HoloLens)
Use SkiaSharp
dotnet add package SkiaSharp --version 1.59.3
ASP. NET Verification Code
Use SkiaSharp to implement text plotting. The Code is as follows:
internal static byte[] GetCaptcha(string captchaText) { byte[] imageBytes = null; int image2d_x = 0; int image2d_y = 0; SKRect size; int compensateDeepCharacters = 0; using (SKPaint drawStyle = CreatePaint()) { compensateDeepCharacters = (int)drawStyle.TextSize / 5; if (System.StringComparer.Ordinal.Equals(captchaText, captchaText.ToUpperInvariant())) compensateDeepCharacters = 0; size = SkiaHelpers.MeasureText(captchaText, drawStyle); image2d_x = (int)size.Width + 10; image2d_y = (int)size.Height + 10 + compensateDeepCharacters; } using (SKBitmap image2d = new SKBitmap(image2d_x, image2d_y, SKColorType.Bgra8888, SKAlphaType.Premul)) { using (SKCanvas canvas = new SKCanvas(image2d)) { canvas.DrawColor(SKColors.Black); // Clear using (SKPaint drawStyle = CreatePaint()) { canvas.DrawText(captchaText, 0 + 5, image2d_y - 5 - compensateDeepCharacters, drawStyle); } using (SKImage img = SKImage.FromBitmap(image2d)) { using (SKData p = img.Encode(SKEncodedImageFormat.Png, 100)) { imageBytes = p.ToArray(); } } } } return imageBytes; }
ASP. NET Core output image:
[HttpGet("/api/captcha")]public IActionResult Captcha(){ var bytes = SkiaCaptcha.Captcha.GetCaptcha("hello world"); return File(bytes, "image/png");}
Reference
Https://skia.org/index_zh
Https://github.com/mono/SkiaSharp
Https://developer.xamarin.com/api/namespace/SkiaSharp/
Demo address: https://github.com/maxzhang1985/ASPNETCore_Captcha_Skia
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.