Using ZKWeb.System.Drawing to implement the authentication Code function (graphics verification code) in the. Net Core _ Practical Tips

Source: Internet
Author: User
Tags git clone

This article describes the implementation of the authentication code with the third party ZKWeb.System.Drawing under. Net Core.

The system passed the test:

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

You can implement the following features:

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.

No.1 project introduced into ZKWeb.System.Drawing

NuGet introduced the package, will not be their own Baidu.

No.2 Simple Verification Code generation

int codew = 80;
int Codeh = 30;
int fontsize = 16;
Random rnd = new Random (); Color list, for verifying code, noise line, noise color[] color = {Color.Black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, color.br
Own, color.darkblue};
Font list, used to verify code string[] Font = {"Times New Roman"}; The character set of the captcha, removing some confusing characters//writing session, verifying 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 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 *, (FloaT) 0);
///write the CAPTCHA picture to the memory stream and output it in "image/png" format MemoryStream ms = new MemoryStream (); try {bmp.
Save (MS, Imageformat.png); Return Ms.
ToArray (); catch (Exception) {return null;} finally {g.dispose (); BMP.
Dispose (); }

No.3 Publish deployment Run

Directly above the picture, will not look here. Net Core Ubuntu 14.04 Department process (graphic)

Note: Verify code 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 libgdiplus
cd/usr/lib
ln-s libgdiplus.so

Fedora 23:

DNF Install Libgdiplus
cd/usr/lib64/
ln-s libgdiplus.so.0

CentOS 7:

Yum install autoconf automake libtool yum install freetype-devel fontconfig libxft-devel yum install
Libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel yum install glib2-devel cairo-devel
git clone https://github.com/mono/libgdiplus
cd libgdiplus
./autogen.sh make make
install
cd/usr/lib64/
ln-s/usr/local/lib/libgdiplus.so Gdiplus.dll

The above is a small set to introduce the. Net Core under the use of ZKWeb.System.Drawing to implement the 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 everyone. Here also thank you very much for the cloud Habitat Community website support!

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.