C#如何消除驗證碼圖片的鋸齒效果
引言
基于生成圖片實現(xiàn)了一個手機號轉(zhuǎn)圖片的需求。 內(nèi)容也很簡單,直接用手機號生成一個png圖片。就是為了背景透明以便其他地方調(diào)用。 有無鋸齒主要依靠一句代碼:g.TextRenderingHint= TextRenderingHint.AntiAlias;
生成圖片
1、有鋸齒
2、無鋸齒
生成方法
string color = "#ff6633"; System.Drawing.Bitmap image = new System.Drawing.Bitmap(170, 35); Graphics g = Graphics.FromImage(image); try { g.TextRenderingHint= TextRenderingHint.AntiAlias; //消除鋸齒 //生成隨機生成器 Random random = new Random(); //清空圖片背景色 //g.Clear(Color.Transparent); //畫圖片的背景噪音線 /*for (int i = 0; i < 2; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); } */ System.Drawing.ColorConverter colConvert = new System.Drawing.ColorConverter(); Color fontColor =(System.Drawing.Color)colConvert.ConvertFromString(color); Font font = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Bold); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), fontColor, fontColor,LinearGradientMode.Horizontal); g.DrawString(phone, font, brush, 2, 2); //畫圖片的前景噪音點 //for (int i = 0; i < 50; i++) //{ // int x = random.Next(image.Width); // int y = random.Next(image.Height); // image.SetPixel(x, y, Color.FromArgb(random.Next())); //} //畫圖片的邊框線 //g.DrawRectangle(new Pen(Color.White), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); Color backColor = image.GetPixel(1, 1); image.MakeTransparent(backColor); image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); context.Response.ClearContent(); context.Response.ContentType = "image/x-png"; context.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); }
參考資料
http://www.blue1000.com/bkhtml/c17/2013-03/71115.htm
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#客戶端程序Visual Studio遠程調(diào)試的方法詳解
這篇文章主要給大家介紹了關于C#客戶端程序Visual Studio遠程調(diào)試的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09C#命令行參數(shù)解析庫System.CommandLine使用
System.CommandLine是一個基于.Net Standard 2.0的命令行參數(shù)解析庫,該項目還是屬于beta狀態(tài),期待以后的正式版本,文章通過示例代碼給大家介紹了System.CommandLine使用講解,感興趣的朋友一起看看吧2021-06-06C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的方式
這篇文章主要給大家介紹了關于C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08解決C#中WebBrowser的DocumentCompleted事件不執(zhí)行的實現(xiàn)方法
本篇文章是對C#中WebBrowser的DocumentCompleted事件不執(zhí)行解決方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05C# List實現(xiàn)行轉(zhuǎn)列的通用方案
本篇通過行轉(zhuǎn)列引出了System.Linq.Dynamic,并且介紹了過濾功能,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03