Net Core 生成圖形驗證碼
阿新 • • 發佈:2018-05-13
max 輸出 from sys 驗證碼 format bitmap reg length
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 //生成驗證碼字符串 16for (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的功能還不只這些 英文水平有限好多屬性我沒沒搞明白是什麽,比如如何內容旋轉啥的 呵呵。。 先記錄這些
Net Core 生成圖形驗證碼