C#.Net根据文字随机生成不同背景的Logo,顾名思义,本篇主题就是在C#.Net中如何根据文字生成小LOGO图像,

在App显示默认头像以及网站默认头像方面有很大应用,如果用户没有上传头像,又不想让每个没有头像的用户都使用同一个默认头像的话,可以根据用户名等随机生成不同背景不同字体的文字LOGO。


使用不同的默认头像不仅让网站或者APP看起来清新,而且给用户留下一个好印象

试想一下,一个网站里没有头像的用户达到70%,如果这70%的用户都使用默认头像,在网站里将会显示出基本都是默认头像,会让网站看起来没有生机,

如果每个用户的头像都不一样 或者都很有规律的话,就会打破这种没有生机的局面。


来张图片展示一下(根据需求可自行调节LOGO尺寸、色调、字体等)

Net根据文字随机生成不同背景的Logo


下面上代码来看下C#.Net中如何轻松搞定根据文字随机生成不同背景的LOGO


C#随机生成不同背景样式的文字logo


01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Web;
05using System.Web.UI;
06using System.Web.UI.WebControls;
07using System.IO;
08 
09namespace AutoLogo
10{
11    /// <summary>
12    /// 随机生成不同背景样式的文字logo
13    /// 来自:www.jsons.cn  Json在线工具
14    /// </summary>
15    public partial class Default : System.Web.UI.Page
16    {
17        public string logoname = string.Empty;
18        protected void Page_Load(object sender, EventArgs e)
19        {
20            if (!IsPostBack)
21            {
22                string extend = "png", Name = "工具";
23                string fileName = DateTime.Now.Ticks.ToString();
24                string RootDir = Server.MapPath("/UploadFile/");//硬盘绝对路径
25                try
26                {
27                    if (string.IsNullOrEmpty(fileName))
28                        fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
29 
30                    string saveFileName = fileName   "."   extend;
31                    Stream stream = Request.InputStream;
32                    //确定文件路径
33                    string fileAddr = "/"   DateTime.Now.ToString("yyyyMMdd")   "/";
34                    #region 生成默认logo
35                    try
36                    {
37                        if (string.IsNullOrWhiteSpace(Name))
38                        {
39                            Response.Write("0");
40                            return;
41                        }
42                        var defImgstream = AutoLogoHelper.CreateLogoImage(Name);
43                        defImgstream.Position = 0;
44                        byte[] datastr = new byte[defImgstream.Length];
45                        defImgstream.Read(datastr, 0, datastr.Length);
46                        if (!File.Exists(RootDir   fileAddr))
47                            Directory.CreateDirectory(RootDir   fileAddr);
48                        File.WriteAllBytes(RootDir   fileAddr   saveFileName, datastr);
49                        logoname = "/UploadFile"   fileAddr   saveFileName;
50 
51                        defImgstream.Close();
52                        defImgstream.Dispose();
53                    }
54                    catch { }
55                    return;
56                    #endregion
57                }
58                catch { }
59            }
60        }
61    }
62}
生成图片流对象:上面代码中的图片生成帮助类,生成LOGO的尺寸、色调、字体等均在此类中调节



001using System;
002using System.Collections.Generic;
003using System.Linq;
004using System.Web;
005using System.Drawing;
006using System.IO;
007 
008namespace AutoLogo
009{
010    public class AutoLogoHelper
011    {
012        /// <summary>
013        /// 生成图片流对象
014        /// 来自:www.jsons.cn  Json在线工具
015        /// </summary>
016        /// <param name="Name">名称</param>
017        /// <returns></returns>
018        public static Stream CreateLogoImage(string Name)
019        {
020            int sizefint = 65;
021            Stream imgstream = new MemoryStream();
022            try
023            {
024                Name = Name.Trim();
025                if (Name.Length >= 3)
026                {
027                    Name = Name.Substring(1, 2);
028                }
029                string FontType = "方正正粗黑简体";
030                Font theFont = new Font(FontType, sizefint);
031                //背景颜色
032                Color col2 = Color.FromArgb(246, 191, 39);//橙色
033                Color col3 = Color.FromArgb(96, 203, 249);//蓝色
034                Color col4 = Color.FromArgb(254, 0, 0);//红色
035                Color col = Color.FromArgb(118, 191, 112);//绿色
036                Random ran = new Random();
037                switch (ran.Next(1, 4))
038                {
039                    case 1:
040                        col = col2;
041                        break;
042                    case 2:
043                        col = col3;
044                        break;
045                    case 3:
046                        col = col4;
047                        break;
048                    default:
049                        col = Color.FromArgb(118, 191, 112);
050                        break;
051                }
052                //文字颜色
053                Color coldef = Color.FromArgb(255, 255, 255);
054                Brush newBrush = new SolidBrush(coldef);
055                int int_ImageWidth = 0;
056                int fontwidth = 110;
057 
058                if (Name.Length > 0)
059                {
060                    if (Name.Length < 4)
061                    {
062                        int_ImageWidth = Name.Length * fontwidth;
063                    }
064                    else
065                    {
066                        int_ImageWidth = Name.Length * fontwidth / 2;
067                    }
068                    Random newRandom = new Random();
069                    //图高20px
070                    Bitmap theBitmap = new Bitmap(int_ImageWidth, int_ImageWidth);
071                    Graphics theGraphics = Graphics.FromImage(theBitmap);
072                    SizeF ziSizeF = new SizeF();
073                    ziSizeF = theGraphics.MeasureString(Name, theFont); //获取文字宽高
074                    //背景色
075                    theGraphics.Clear(col);
076                    //10pt的字体分行
077                    string sInput = Name; //获取用户名称
078                    int CodeLength = Name.Length; //获取用户名字长度
079                    int coloum = 3; //第一行显示字数 (如果用户名字长度=2到3个字显示一行,大于三个字显示2行)
080                    if (CodeLength > 3)
081                    {
082                        coloum = 2;
083                    }
084                    if (CodeLength < 3)
085                    {
086                        coloum = CodeLength;
087                    }
088                    //利用循环,来依次输出
089                    for (int i = 0, j = 0; i < sInput.Length; i  = coloum, j  )
090                    {
091                        if (j == 0)
092                        {
093                            string s = sInput.Substring(i, coloum);
094                            theGraphics.DrawString(s, theFont, newBrush, (int_ImageWidth - ziSizeF.Width) / 2,
095                                (int_ImageWidth - ziSizeF.Height) / 2   10);
096                        }
097                        else if (j == 1)
098                        {
099                            string s = sInput.Substring(i, sInput.Length - coloum);
100                            theGraphics.DrawString(s, theFont, newBrush, (int_ImageWidth - ziSizeF.Width / 2) / 2 - 6,
101                                int_ImageWidth / 2   (int_ImageWidth / 2 - ziSizeF.Height) / 2);
102                        }
103                    }
104                    System.Drawing.Image bitmap = new System.Drawing.Bitmap(80, 80); //新建bmp图片
105                    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //新建画板
106                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //制定高质量插值法
107                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量、低速度呈现平滑程度
108                    g.Clear(System.Drawing.Color.White); //清空画布
109                    //在制定位置画图
110                    g.DrawImage(theBitmap, new System.Drawing.Rectangle(0, 0, 80, 80),
111                        new System.Drawing.Rectangle(0, 0, int_ImageWidth, int_ImageWidth),
112                        System.Drawing.GraphicsUnit.Pixel);
113                    //图片转换成流
114                    bitmap.Save(imgstream, System.Drawing.Imaging.ImageFormat.Png);
115                    theGraphics.Dispose();
116                    theBitmap.Dispose();
117                }
118            }
119            catch { }
120            return imgstream;
121        }
122    }
123}


好了,废话不多说了,如有需求可下载下面DEMO源码自行研究扩展。

Net根据文字随机生成不同背景的Logo源码DEMO下载

原文链接:Net根据文字随机生成不同背景的Logo源码DEMO下载