C#.Net根據(jù)文字隨機(jī)生成不同背景的Logo,顧名思義,本篇主題就是在C#.Net中如何根據(jù)文字生成小LOGO圖像,
在App顯示默認(rèn)頭像以及網(wǎng)站默認(rèn)頭像方面有很大應(yīng)用,如果用戶沒有上傳頭像,又不想讓每個(gè)沒有頭像的用戶都使用同一個(gè)默認(rèn)頭像的話,可以根據(jù)用戶名等隨機(jī)生成不同背景不同字體的文字LOGO。
使用不同的默認(rèn)頭像不僅讓網(wǎng)站或者APP看起來清新,而且給用戶留下一個(gè)好印象
試想一下,一個(gè)網(wǎng)站里沒有頭像的用戶達(dá)到70%,如果這70%的用戶都使用默認(rèn)頭像,在網(wǎng)站里將會顯示出基本都是默認(rèn)頭像,會讓網(wǎng)站看起來沒有生機(jī),
如果每個(gè)用戶的頭像都不一樣 或者都很有規(guī)律的話,就會打破這種沒有生機(jī)的局面。
來張圖片展示一下(根據(jù)需求可自行調(diào)節(jié)LOGO尺寸、色調(diào)、字體等)
下面上代碼來看下C#.Net中如何輕松搞定根據(jù)文字隨機(jī)生成不同背景的LOGO
C#隨機(jī)生成不同背景樣式的文字logo
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace AutoLogo { /// <summary> /// 隨機(jī)生成不同背景樣式的文字logo /// 來自:hnxxbl.cn Json在線工具 /// </summary> public partial class Default : System.Web.UI.Page { public string logoname = string.Empty; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string extend = "png", Name = "工具"; string fileName = DateTime.Now.Ticks.ToString(); string RootDir = Server.MapPath("/UploadFile/");//硬盤絕對路徑 try { if (string.IsNullOrEmpty(fileName)) fileName = DateTime.Now.ToString("yyyyMMddHHmmss"); string saveFileName = fileName "." extend; Stream stream = Request.InputStream; //確定文件路徑 string fileAddr = "/" DateTime.Now.ToString("yyyyMMdd") "/"; #region 生成默認(rèn)logo try { if (string.IsNullOrWhiteSpace(Name)) { Response.Write("0"); return; } var defImgstream = AutoLogoHelper.CreateLogoImage(Name); defImgstream.Position = 0; byte[] datastr = new byte[defImgstream.Length]; defImgstream.Read(datastr, 0, datastr.Length); if (!File.Exists(RootDir fileAddr)) Directory.CreateDirectory(RootDir fileAddr); File.WriteAllBytes(RootDir fileAddr saveFileName, datastr); logoname = "/UploadFile" fileAddr saveFileName; defImgstream.Close(); defImgstream.Dispose(); } catch { } return; #endregion } catch { } } } } }生成圖片流對象:上面代碼中的圖片生成幫助類,生成LOGO的尺寸、色調(diào)、字體等均在此類中調(diào)節(jié)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.IO; namespace AutoLogo { public class AutoLogoHelper { /// <summary> /// 生成圖片流對象 /// 來自:hnxxbl.cn Json在線工具 /// </summary> /// <param name="Name">名稱</param> /// <returns></returns> public static Stream CreateLogoImage(string Name) { int sizefint = 65; Stream imgstream = new MemoryStream(); try { Name = Name.Trim(); if (Name.Length >= 3) { Name = Name.Substring(1, 2); } string FontType = "方正正粗黑簡體"; Font theFont = new Font(FontType, sizefint); //背景顏色 Color col2 = Color.FromArgb(246, 191, 39);//橙色 Color col3 = Color.FromArgb(96, 203, 249);//藍(lán)色 Color col4 = Color.FromArgb(254, 0, 0);//紅色 Color col = Color.FromArgb(118, 191, 112);//綠色 Random ran = new Random(); switch (ran.Next(1, 4)) { case 1: col = col2; break; case 2: col = col3; break; case 3: col = col4; break; default: col = Color.FromArgb(118, 191, 112); break; } //文字顏色 Color coldef = Color.FromArgb(255, 255, 255); Brush newBrush = new SolidBrush(coldef); int int_ImageWidth = 0; int fontwidth = 110; if (Name.Length > 0) { if (Name.Length < 4) { int_ImageWidth = Name.Length * fontwidth; } else { int_ImageWidth = Name.Length * fontwidth / 2; } Random newRandom = new Random(); //圖高20px Bitmap theBitmap = new Bitmap(int_ImageWidth, int_ImageWidth); Graphics theGraphics = Graphics.FromImage(theBitmap); SizeF ziSizeF = new SizeF(); ziSizeF = theGraphics.MeasureString(Name, theFont); //獲取文字寬高 //背景色 theGraphics.Clear(col); //10pt的字體分行 string sInput = Name; //獲取用戶名稱 int CodeLength = Name.Length; //獲取用戶名字長度 int coloum = 3; //第一行顯示字?jǐn)?shù) (如果用戶名字長度=2到3個(gè)字顯示一行,大于三個(gè)字顯示2行) if (CodeLength > 3) { coloum = 2; } if (CodeLength < 3) { coloum = CodeLength; } //利用循環(huán),來依次輸出 for (int i = 0, j = 0; i < sInput.Length; i = coloum, j ) { if (j == 0) { string s = sInput.Substring(i, coloum); theGraphics.DrawString(s, theFont, newBrush, (int_ImageWidth - ziSizeF.Width) / 2, (int_ImageWidth - ziSizeF.Height) / 2 10); } else if (j == 1) { string s = sInput.Substring(i, sInput.Length - coloum); theGraphics.DrawString(s, theFont, newBrush, (int_ImageWidth - ziSizeF.Width / 2) / 2 - 6, int_ImageWidth / 2 (int_ImageWidth / 2 - ziSizeF.Height) / 2); } } System.Drawing.Image bitmap = new System.Drawing.Bitmap(80, 80); //新建bmp圖片 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //新建畫板 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //制定高質(zhì)量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //設(shè)置高質(zhì)量、低速度呈現(xiàn)平滑程度 g.Clear(System.Drawing.Color.White); //清空畫布 //在制定位置畫圖 g.DrawImage(theBitmap, new System.Drawing.Rectangle(0, 0, 80, 80), new System.Drawing.Rectangle(0, 0, int_ImageWidth, int_ImageWidth), System.Drawing.GraphicsUnit.Pixel); //圖片轉(zhuǎn)換成流 bitmap.Save(imgstream, System.Drawing.Imaging.ImageFormat.Png); theGraphics.Dispose(); theBitmap.Dispose(); } } catch { } return imgstream; } } }
好了,廢話不多說了,如有需求可下載下面DEMO源碼自行研究擴(kuò)展。
Net根據(jù)文字隨機(jī)生成不同背景的Logo源碼DEMO下載
原文鏈接:Net根據(jù)文字隨機(jī)生成不同背景的Logo源碼DEMO下載