Using ZKWeb.System.Drawing to implement CAPTCHA functionality under. Net Core

Source: Internet
Author: User
This article describes the use of third-party ZKWeb.System.Drawing to implement CAPTCHA functionality under. Net Core.

The system passed the test:

Windows 8.1 64bit
Ubuntu Server 16.04 LTS 64bit
Fedora 64bit
CentOS 7.2 64bit

The following functions can be implemented:

Open jpg, BMP, ICO, PNG
Save jpg, BMP, ICO, PNG
Resize image
Draw graphics with brush and pen
Open font and draw string

The above is the official information.

Introduction of the ZKWeb.System.Drawing project

NuGet introduces the package and will not own Baidu.

No.2 Simple Verification Code generation

int Codew = 80;int Codeh = 30;int fontSize = 16; Random rnd = new Random ();//Color list, for captcha, noise, noise color[] color = {Color.Black, color.red, Color.Blue, Color.green, Color.orang E, Color.brown, Color.brown, color.darkblue};//font list, for captcha string[] Font = {"Times New Roman"};//the character set of the verification code, remove some easily confusing characters//write Enter session, verify code encryption//webhelper.writesession ("Session_verifycode", Md5helper.md5 (Chkcode.tolower (), 16));//Create Canvas bitmap BMP = New Bitmap (Codew, Codeh); Graphics g = graphics.fromimage (BMP); G.clear (color.white);//Draw noise line for (int i = 0; i < 1; i++) {int x1 = rnd. Next (codew); int y1 = rnd. Next (CODEH); int x2 = rnd. Next (codew); int y2 = rnd. Next (Codeh); Color clr = color[rnd. Next (color. Length)];g.drawline (new Pen (CLR), x1, y1, x2, y2);} Draw the CAPTCHA string for (int i = 0; i < chkcode.length; i++) {string fnt = Font[rnd. Next (font. Length)]; Font ft = new Font (FNT, fontSize); Color clr = color[rnd. Next (color. Length)];g.drawstring (Chkcode[i]. ToString (), FT, new SolidBrush (CLR), (float) i *, (float) 0);} Writes the CAPTCHA picture to the memory stream and takes it as "image/png"Format output MemoryStream ms = new MemoryStream (); try{bmp. Save (MS, imageformat.png); return Ms. ToArray ();} catch (Exception) {return null;} Finally{g.dispose (); BMP. Dispose ();}

No.3 release deployment Run

Directly, won't look here. Net Core Ubuntu 14.04 deployment process (graphic)

Note: Verification code under Windows generates no pressure, I use Ubuntu 14, need to install GDI package, run log will be prompted.

Installation method:

Ubuntu 16.04:

Apt-get Install libgdipluscd/usr/libln-s libgdiplus.so gdiplus.dll

Fedora 23:

DNF Install Libgdipluscd/usr/lib64/ln-s libgdiplus.so.0 Gdiplus.dll

CentOS 7:

Yum Install autoconf automake libtoolyum install freetype-devel fontconfig libxft-develyum install libjpeg-turbo-devel li Bpng-devel giflib-devel libtiff-devel libexif-develyum install glib2-devel cairo-develgit clone https://github.com/ MONO/LIBGDIPLUSCD libgdiplus./autogen.shmakemake installcd/usr/lib64/ln-s/usr/local/lib/libgdiplus.so Gdiplus.dll

The above is a small part of the introduction of the. Net Core using ZKWeb.System.Drawing Implementation Verification Code function (graphics verification code), I hope to help you, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!

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