C#如何消除驗(yàn)證碼圖片的鋸齒效果
引言
基于生成圖片實(shí)現(xiàn)了一個(gè)手機(jī)號(hào)轉(zhuǎn)圖片的需求。 內(nèi)容也很簡(jiǎn)單,直接用手機(jī)號(hào)生成一個(gè)png圖片。就是為了背景透明以便其他地方調(diào)用。 有無(wú)鋸齒主要依靠一句代碼:g.TextRenderingHint= TextRenderingHint.AntiAlias;
生成圖片
1、有鋸齒
2、無(wú)鋸齒
生成方法
string color = "#ff6633"; System.Drawing.Bitmap image = new System.Drawing.Bitmap(170, 35); Graphics g = Graphics.FromImage(image); try { g.TextRenderingHint= TextRenderingHint.AntiAlias; //消除鋸齒 //生成隨機(jī)生成器 Random random = new Random(); //清空?qǐng)D片背景色 //g.Clear(Color.Transparent); //畫(huà)圖片的背景噪音線 /*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); //畫(huà)圖片的前景噪音點(diǎn) //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())); //} //畫(huà)圖片的邊框線 //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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:WPF 自定義雷達(dá)圖開(kāi)發(fā)實(shí)例教程
欄 目:C#教程
下一篇:解決C#中Linq GroupBy 和OrderBy失效的方法
本文標(biāo)題:C#如何消除驗(yàn)證碼圖片的鋸齒效果
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6256.html
您可能感興趣的文章
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10關(guān)于nancy中的身份驗(yàn)證
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單離線注冊(cè)碼生成與驗(yàn)證
- 01-10C#身份證號(hào)碼驗(yàn)證是否正確
- 01-10C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
- 01-10c#實(shí)現(xiàn)識(shí)別圖片上的驗(yàn)證碼數(shù)字
- 01-1012306奇葩驗(yàn)證碼引發(fā)思考之C#實(shí)現(xiàn)驗(yàn)證碼程序
- 01-10基于C#實(shí)現(xiàn)12306的動(dòng)態(tài)驗(yàn)證碼變成靜態(tài)驗(yàn)證碼的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文