ASP. NET Core sample code for implementing verification code using SkiaSharp

  • 2021-10-24 19:22:37
  • OfStack

Preface

This article does not implement a completed verification code sample, but provides another idea of using Drawing API under the current. NET Core 2.0, and shows it in the form of simple Demo.

Skia

Skia is an open source 2D graphics library that provides a variety of commonly used API and can run on a variety of software and hardware platforms. Google's Chrome browser, Chrome OS, Android, Firefox browser, Firefox operating system and many other products use it as a graphics engine.

Skia is funded and managed by Google, and anyone can use Skia based on BSD free software license. The Skia development team is committed to developing its core parts and widely incorporates the open source contributions of all parties to Skia.

SkiaSharp

SkiaSharp is a cross-platform 2D graphics. NET API binding initiated by Mono and based on Google's Skia graphics library. Provides a comprehensive 2D API for graphics rendering and image processing across mobile, server and desktop modes.

skiasharp provides PCL and platform-specific bindings:

.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)

Using SkiaSharp


dotnet add package SkiaSharp --version 1.59.3

ASP. NET verification code

Before using SkiaSharp to achieve text drawing function, 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


Related articles: