博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net Core 之 图形验证码 本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。...
阅读量:7124 次
发布时间:2019-06-28

本文共 2217 字,大约阅读时间需要 7 分钟。

本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。

通过测试的系统:Windows 8.1 64bitUbuntu Server 16.04 LTS 64bitFedora 24 64bitCentOS 7.2 64bit可以实现以下功能:Open jpg, bmp, ico, pngSave jpg, bmp, ico, pngResize imageDraw graphics with brush and penOpen font and draw string

以上是官方给的资料。


No.1 项目引入ZKWeb.System.Drawing

NuGet引入包,不会的自己百度。


No.2 简单的验证码生成

int codeW = 80;int codeH = 30;int fontSize = 16;Random rnd = new Random();//颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };//字体列表,用于验证码 string[] font = { "Times New Roman" };//验证码的字符集,去掉了一些容易混淆的字符 //写入Session、验证码加密//WebHelper.WriteSession("session_verifycode", Md5Helper.MD5(chkCode.ToLower(), 16));//创建画布Bitmap bmp = new Bitmap(codeW, codeH);Graphics g = Graphics.FromImage(bmp);g.Clear(Color.White);//画噪线 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);}//画验证码字符串 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);}//将验证码图片写入内存流,并将其以 "image/Png" 格式输出 MemoryStream ms = new MemoryStream();try{    bmp.Save(ms, ImageFormat.Png);    return ms.ToArray();}catch (Exception){    return null;}finally{    g.Dispose();    bmp.Dispose();}

No.3 发布部署运行

直接上图,不会的看这里


注意:验证码Windows下生成无压力,我用的Ubuntu 14,需要安装gdi包,运行日志中会有提示。

安装方法:

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

 

 

88..

转载于:https://www.cnblogs.com/webenh/p/6185084.html

你可能感兴趣的文章
Android中Bitmap和Drawable
查看>>
python中 time模块的学习
查看>>
使用xshell登录中文版CentOS时,在xshell下显示中文乱码,通过如下办法可以有效的解决乱码问题...
查看>>
【转】Js基础知识(五) - 前端性能优化总结
查看>>
我的友情链接
查看>>
python之day2
查看>>
邮件服务器架构基础
查看>>
【白话设计模式十四】代理模式(Proxy)
查看>>
LINUX内核经典面试题
查看>>
解决连表查询连接字段字符集不一致异常
查看>>
网页基础
查看>>
springboot(五):spring data jpa的使用
查看>>
导数 sobel
查看>>
Debian命令行的配制工具
查看>>
my tips
查看>>
BeanUtils包的使用
查看>>
python学习笔记之函数
查看>>
敏捷个人教你如何制作2012生活看板
查看>>
JDBC连接时,出现的错误ORA-00604
查看>>
ftp: connect: No route to host
查看>>