截止.Net Core 2.0 目前官方類庫的API中不支持Bitmap
System.Drawing.Primitives 這是官方的一個Drawing庫,但是沒有Bitmap、Graphics等很多東西,所以這個做圖形驗證碼基本Pass了。
zkweb.system.drawing 這個是第三方的,從mono的System.Drawing修改得來的。過程比較詳細,也實現了。所以把這個的使用跟大家分享一下。
截止.Net Core 3.0 自帶框架中不支持Drawing庫
可以推薦使用System.Drawing.Common 微軟提供的GDI+ 類庫
安裝命令:
Install-Package ZKWeb.System.Drawing
常用的操作,基本上和GDI+相同
實例1,修改圖片為JPEG 格式,能縮小圖片的大小而且相對比較清晰
static void ImgTestTwo() { string filename = @"E:\CoreWork\backone.jpg"; string targetname = @"E:\CoreWork\temp1.jpg"; Bitmap bitmap = new Bitmap(filename); //保持圖片的比例不變,縮放圖片 int width = 1000, height = 1000; if (bitmap.Width > bitmap.Height) { //寬度為大,計算高度 height = Convert.ToInt32(width * (bitmap.Height * 1.0 / bitmap.Width)); } else { //高度為大,計算寬度 width = Convert.ToInt32(height * (bitmap.Width * 1.0 / bitmap.Height)); } Bitmap result = ResizeImage(bitmap, width, height); filename = filename.Substring(0,filename.LastIndexOf('.'))+".jpg"; //保存圖片,指定保存 格式為Jpeg,占用空間會比較小 result.Save(targetname,ImageFormat.Jpeg); result.Dispose(); bitmap.Dispose(); } /// <summary> /// Resize圖片 /// </summary> /// <param name="bmp">原始Bitmap </param> /// <param name="newW">新的寬度</param> /// <param name="newH">新的高度</param> /// <returns>處理以后的圖片</returns> public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH) { try { Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的質量 //g.InterpolationMode = InterpolationMode.NearestNeighbor; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch(Exception ex) { return null; } }
示例2.生成圖片驗證碼:
1 public class VierificationCodeServices 2 { 3 /// <summary> 4 /// 該方法用於生成指定位數的隨機數 5 /// </summary> 6 /// <param name="VcodeNum">參數是隨機數的位數</param> 7 /// <returns>返回一個隨機數字符串</returns> 8 private string RndNum(int VcodeNum) 9 { 10 //驗證碼可以顯示的字符集合 11 string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p" + 12 ",q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,P,Q" + 13 ",R,S,T,U,V,W,X,Y,Z"; 14 string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成數組 15 string code = "";//產生的隨機數 16 int temp = -1;//記錄上次隨機數值,盡量避避免生產幾個一樣的隨機數 17 18 Random rand = new Random(); 19 //采用一個簡單的算法以保證生成隨機數的不同 20 for (int i = 1; i < VcodeNum + 1; i++) 21 { 22 if (temp != -1) 23 { 24 rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));//初始化隨機類 25 } 26 int t = rand.Next(61);//獲取隨機數 27 if (temp != -1 && temp == t) 28 { 29 return RndNum(VcodeNum);//如果獲取的隨機數重復,則遞歸調用 30 } 31 temp = t;//把本次產生的隨機數記錄起來 32 code += VcArray[t];//隨機數的位數加一 33 } 34 return code; 35 } 36 37
/// <summary> /// 生成圖片驗證碼 /// </summary> /// <param name="code"></param> /// <returns></returns> public static MemoryStream ValideCode(string code) { Bitmap Img = null; Graphics g = null; Random random = new Random(); //驗證碼顏色集合 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //驗證碼字體集合 string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" }; //定義圖像的大小,生成圖像的實例 Img = new Bitmap((int)code.Length * 18, 32); g = Graphics.FromImage(Img);//從Img對象生成新的Graphics對象 g.Clear(Color.White);//背景設為白色 //在隨機位置畫背景點 for (int i = 0; i < 100; i++) { int x = random.Next(Img.Width); int y = random.Next(Img.Height); g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } //驗證碼繪制在g中 for (int i = 0; i < code.Length; i++) { int cindex = random.Next(7);//隨機顏色索引值 int findex = random.Next(5);//隨機字體索引值 Font f = new Font(fonts[findex], 15, FontStyle.Bold);//字體 Brush b = new SolidBrush(c[cindex]);//顏色 int ii = 4; if ((i + 1) % 2 == 0)//控制驗證碼不在同一高度 { ii = 2; } g.DrawString(code.Substring(i, 1), f, b, 5 + (i * 15), ii);//繪制一個驗證字符 } MemoryStream ms = new MemoryStream();//生成內存流對象 Img.Save(ms, ImageFormat.Jpeg);//將此圖像以Png圖像文件的格式保存到流中 ms.Seek(0, SeekOrigin.Begin);//指針回歸 //回收資源 g.Dispose(); Img.Dispose(); return ms; }
找背景圖參考:http://beijing.gongjuji.net/
更多: