Sample codes for using SkiaSharp to implement verification codes for ASP. NET Core and skiasharp

Source: Internet
Author: User
Tags skia

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:

  1. . NET Core/. NET Standard 1.3
  2. Xamarin. Android
  3. Xamarin. iOS
  4. Xamarin. tvOS
  5. Xamarin. Mac
  6. Windows Classic Desktop (Windows. Forms/WPF)
  7. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.