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!