In. Net Core, ZKWeb. System. Drawing is used to implement the verification code function (graphical verification code). netcoredrawing

Source: Internet
Author: User

In. Net Core, ZKWeb. System. Drawing is used to implement the verification code function (graphical verification code). netcoredrawing

This article describes how to use a third-party ZKWeb. System. Drawing in. Net Core to implement the verification code function.

Tested systems:

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

You can implement the following functions:

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 are official materials.

No.1 introduce ZKWeb. System. Drawing

NuGet introduces the package and won't be Baidu.

No. 2 simple verification code generation

Int codeW = 80; int codeH = 30; int fontSize = 16; Random rnd = new Random (); // color list, used for verification codes, noise lines, and noise Color [] color = {Color. black, Color. red, Color. blue, Color. green, Color. orange, Color. brown, Color. brown, Color. darkBlue}; // font list, used for verification code string [] font = {"Times New Roman"}; // Character Set of the verification code, some confusing characters are removed. // write Session and verification code encryption // WebHelper. writeSession ("session_verifycode", Md5Helper. MD5 (chkCode. toLower (), 16); // create the canvas Bitmap bmp = new Bitmap (codeW, codeH); Graphics g = Graphics. fromImage (bmp); g. clear (Color. white); // draw the 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 verification code 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 * 18, (float) 0);} // write the verification code image to the memory stream, and 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, and Operation

For more information, see. Net Core's Ubuntu 14.04 deployment process)

Note: There is no pressure to generate the verification code in Windows. For Ubuntu 14, we need to install the gdi package. A prompt is displayed in the running log.

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 libpng-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 series for you to introduce. use ZKWeb. Net Core. system. drawing implements the verification code function (Graphic Verification Code). I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.