一来工作需要,而来我也一直有兴趣自己写一个长微博工具,其实思想很简单,首先文转图,然后拼图。下面来贴上自己写的文转图代码,以供大家研究参考。
下面这段是我封装的实现文转图的代码,里边对某个字体实际占的位符没有深入研究,按照微博的宽度440,我测试了字体为16,20,28 的占位符,有兴趣的朋友可以告知一下,有什么规律.
/// <summary>
/// 文字转图
/// </summary>
/// <param name="text">文字</param>
/// <param name="fc">文字颜色</param>
/// <param name="fb">图片背景色</param>
/// <param name="fs">字体类型,比如正常,加粗,倾斜</param>
/// <param name="ft">文字类型,比如楷体_GB2312</param>
/// <param name="fn">默认为1; 文字类型名称。1:楷体_GB2312;</param>
/// <param name="fs">默认为1; 文字大小。1:16;2:20;3:24;</param>
/// <param name="hasBlank">默认为1; 是否开始空俩格。1:不空;2:空;</param>
/// <returns></returns>
public Bitmap WordToImg(string text,Color fc,Color fb,FontStyle ft,int fn=1,int fs=2,int hasBlank=1)
{
if (string.IsNullOrEmpty(text))
return null;
int number = 0;
string font_name = "";
int font_size = 14;
int line_add = 30;
int font_size_lenth = 21;
switch (fn)
{
case 1:
font_name = "宋体";
break;
case 2:
font_name = "楷体";
break;
}
switch (fs)
{
case 2:
font_size = 16;
line_add = 35;
font_size_lenth = 23; //楷体是25
break;
case 3:
font_size = 20;
line_add = 45;
font_size_lenth = 28; //楷体是31
break;
case 4:
font_size = 24;
line_add = 55;
font_size_lenth = 35;
break;
}
int row_lentht = Math.Abs(440 / font_size_lenth);
List<string> list = StringWrap(text, row_lentht, hasBlank, out number);
Bitmap image = new Bitmap(440, (number+1)*line_add);
Graphics gh = Graphics.FromImage(image);
int line = 15;
gh.Clear(fb);
Font font = new Font(font_name, font_size, (ft));
System.Drawing.Drawing2D.LinearGradientBrush brush = null;
for (int i = 0; i < number; i++)
{
try
{
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), fc, Color.DarkRed, 1.2f, true);
gh.DrawString(list[i], font, brush, 20, line);
line = line + line_add;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
image.Save(@"D:\工作\调试\ConvertWordsToImg\aa.jpg", ImageFormat.Png);
return image;
}
好啦,下面贴上,将一段文字转换成图片能容纳的一定长度
/// <summary>
/// 将一句话按指定是字符数切开
/// </summary>
/// <param name="str"></param>
/// <param name="num">指定每行字符数</param>
/// <param name="hasBlank">是否开始空俩个</param>
/// <returns></returns>
public List<string> StringWrap(string str, int num,int hasBlank, out int count)
{
List<string> list = new List<string>();
Regex reg = new Regex(@"^[a-z0-9]+{1}quot;);
double number = 0;
int _count = 0;
int _number=0;
StringBuilder sb = new StringBuilder();
if (hasBlank == 1)
sb.Append(" ");
try
{
foreach (char c in str)
{
if (_number == 0 && hasBlank == 1)
number += 2;
bool result = reg.IsMatch(c.ToString());
if (result)
number += 0.5;
else
number++;
sb.Append(c.ToString());
if (number >= num)
{
list.Add(sb.ToString());
sb = new StringBuilder();
number = 0;
_count++;
}
if (_number == str.Length-1)
{
list.Add(sb.ToString());
_count++;
}
_number++;
}
}
catch
{
count = 0;
return null;
}
count = _count;
return list;
}
下面来贴上多张图片按次序拼图的代码,对于图片宽度小于440,按居中处理,大于的超出部分不处理
/// <summary>
/// 微博专用图片合成,因为微博图片宽度限制小于440,我们统一处理为440
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public Bitmap SynthesisImgW(List<Bitmap> bl)
{
int height = 0;
foreach (Bitmap map in bl)
{
height += map.Height+10;
}
Bitmap image = new Bitmap(440, height-10);
Graphics gh = Graphics.FromImage(image);
gh.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gh.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gh.Clear(Color.White);
int y = 0;
foreach (Bitmap map in bl)
{
gh.DrawImage(map, 0,y,map.Width,map.Height);
y += map.Height+10;
}
return image;
}
好了,这三个方法,让你轻松实现文转图,拼图功能,其实很简单,本人也不是什么代码高手,就是好奇写着玩,代码级别哪有错误还请各位看客多多海涵,也欢迎相互交流。