C#.Net根据文字随机生成不同背景的Logo,顾名思义,本篇主题就是在C#.Net中如何根据文字生成小LOGO图像,
在App显示默认头像以及网站默认头像方面有很大应用,如果用户没有上传头像,又不想让每个没有头像的用户都使用同一个默认头像的话,可以根据用户名等随机生成不同背景不同字体的文字LOGO。
使用不同的默认头像不仅让网站或者APP看起来清新,而且给用户留下一个好印象
试想一下,一个网站里没有头像的用户达到70%,如果这70%的用户都使用默认头像,在网站里将会显示出基本都是默认头像,会让网站看起来没有生机,
如果每个用户的头像都不一样 或者都很有规律的话,就会打破这种没有生机的局面。
来张图片展示一下(根据需求可自行调节LOGO尺寸、色调、字体等)
下面上代码来看下C#.Net中如何轻松搞定根据文字随机生成不同背景的LOGO
C#随机生成不同背景样式的文字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>
/// 随机生成不同背景样式的文字logo
/// 来自:www.yunjson.com 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 生成默认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的尺寸、色调、字体等均在此类中调节
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
namespace AutoLogo
{
public class AutoLogoHelper
{
/// <summary>
/// 生成图片流对象
/// 来自:www.yunjson.com 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);//蓝色
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; //第一行显示字数 (如果用户名字长度=2到3个字显示一行,大于三个字显示2行)
if (CodeLength > 3)
{
coloum = 2;
}
if (CodeLength < 3)
{
coloum = CodeLength;
}
//利用循环,来依次输出
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; //制定高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量、低速度呈现平滑程度
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);
//图片转换成流
bitmap.Save(imgstream, System.Drawing.Imaging.ImageFormat.Png);
theGraphics.Dispose();
theBitmap.Dispose();
}
}
catch { }
return imgstream;
}
}
}
好了,废话不多说了,如有需求可下载下面DEMO源码自行研究扩展。