1. NetCore ZKweb
在我第一次绘制图形验证码时是采用的ZKweb的绘制库,奉上代码参考
1 public byte[] GetVerifyCode(out string code)
2 {
3 code = string.Empty;
4 int codeW = 80;
5 int codeH = 30;
6 int fontSize = 16;
7 string chkCode = string.Empty;
8 Random rnd = new Random();
9 //颜色列表,用于验证码、噪线、噪点
10 System.DrawingCore.Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
11 //字体列表,用于验证码
12 string[] font = { "Times New Roman" };
13 //验证码的字符集,去掉了一些容易混淆的字符
14 char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
15 //生成验证码字符串
16 for (int i = 0; i < 4; i++)
17 {
18 chkCode += character[rnd.Next(character.Length)];
19 }
20 code = chkCode;
21
22 //创建画布
23 Bitmap bmp = new Bitmap(codeW, codeH);
24 Graphics g = Graphics.FromImage(bmp);
25 g.Clear(Color.White);
26 //画噪线
27 for (int i = 0; i < 1; i++)
28 {
29 int x1 = rnd.Next(codeW);
30 int y1 = rnd.Next(codeH);
31 int x2 = rnd.Next(codeW);
32 int y2 = rnd.Next(codeH);
33 Color clr = color[rnd.Next(color.Length)];
34 g.DrawLine(new Pen(clr), x1, y1, x2, y2);
35 }
36 //画验证码字符串
37 for (int i = 0; i < chkCode.Length; i++)
38 {
39 string fnt = font[rnd.Next(font.Length)];
40 Font ft = new Font(fnt, fontSize);
41 Color clr = color[rnd.Next(color.Length)];
42 g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
43 }
44 //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
45 MemoryStream ms = new MemoryStream();
46 try
47 {
48 bmp.Save(ms, ImageFormat.Png);
49 return ms.ToArray();
50 }
51 catch (Exception)
52 {
53 return null;
54 }
55 finally
56 {
57 g.Dispose();
58 bmp.Dispose();
59 }
60 }
这个开始用的还不错,但是今天更新了VS到15.7版这个就产生了一个很恶心的问题,ZKweb的引用库和项目自带的Core.All好几个dll冲突,这两个一个是ZKweb的依赖库一个是项目的依赖库 都没法重新引用,百度了很久后决定放弃ZKweb.改用SkiaSharp
2. SkiaSharp
这个百度上的搜索结果没有一个是给了可用代码的。ε=(´ο`*)))唉 而且大部分都是一个人放出来的代码 好吧开始自己整。先上代码
1 public IActionResult Code()
2 {
3 #region 反射SK支持的全部颜色
4 //List<SKColor> colors = new List<SKColor>();
5 //var skcolors = new SKColors();
6 //var type = skcolors.GetType();
7 //foreach (FieldInfo field in type.GetFields())
8 //{
9 // colors.Add( (SKColor)field.GetValue(skcolors));
10 //}
11 #endregion
12
13 //int maxcolorindex = colors.Count-1;
14 string text = "1A3V";
15 var zu = text.ToList();
16 SKBitmap bmp = new SKBitmap(80, 30);
17 using (SKCanvas canvas = new SKCanvas(bmp))
18 {
19 //背景色
20 canvas.DrawColor(SKColors.White);
21
22 using (SKPaint sKPaint = new SKPaint())
23 {
24 sKPaint.TextSize = 16;//字体大小
25 sKPaint.IsAntialias = true;//开启抗锯齿
26 sKPaint.Typeface = SKTypeface.FromFamilyName("微软雅黑", SKTypefaceStyle.Bold);//字体
27 SKRect size = new SKRect();
28 sKPaint.MeasureText(zu[0].ToString(), ref size);//计算文字宽度以及高度
29
30 float temp = (bmp.Width/4 - size.Size.Width)/2;
31 float temp1 = bmp.Height - (bmp.Height - size.Size.Height) / 2;
32 Random random = new Random();
33
34 for (int i = 0; i < 4; i++)
35 {
36
37 sKPaint.Color = new SKColor((byte)random.Next(0,255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
38 canvas.DrawText(zu[i].ToString(), temp + 20*i, temp1, sKPaint);//画文字
39 }
40 //干扰线
41 for (int i = 0; i < 5; i++)
42 {
43 sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
44 canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), sKPaint);
45 }
46 }
47 //页面展示图片
48 using (SKImage img = SKImage.FromBitmap(bmp))
49 {
50 using (SKData p = img.Encode())
51 {
52 return File(p.ToArray(), "image/Png");
53 }
54 }
55 }
56 }
这个最大的好处不依赖任何库,不用担心和core自身的dll冲突问题,SkiaSharp的功能还不只这些 英文水平有限好多属性我没没搞明白是什么,比如如何内容旋转啥的 呵呵。。 先记录这些
写在结尾: 工作不只是工作,还是生活的大部分,生活烦了就需要换个心情
转自:http://Www.CnBlogs.Com/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
| 本博客Android APP 下载 |
![]() |
| 支持我们就给我们点打赏 |
![]() |
| 支付宝打赏 支付宝扫一扫二维码 |
![]() |
| 微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!





浙公网安备 33010602011771号