【思路】
使用GDI将文字绘制到位图上,根据位图上的像素点颜色来转换成对应矩阵位置的字符,即可得到如上图所示的文本。
以下为程序主窗口代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace TextTool
{
public partial class MainForm : Form
{
/// <summary>
/// 构造方法
/// </summary>
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
//初始化信息
this.Text = "文字转字符字工具 " + ProVersion + " By:ls9512";
label5.Text = "输出字号: (" + MinFontSize.ToString() + "-" + MaxFontSize.ToString() + ")";
//获取系统字体列表
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (System.Drawing.FontFamily family in fonts.Families)
{
comboBoxFont.Items.Add(family.Name);
}
DefaultType();
}
//全局变量
private string ProVersion = "v1.0";
private int MaxFontSize = 100;
private int MinFontSize = 1;
/// <summary>
/// 转换
/// </summary>
private void ConvertType()
{
try
{
string text = "";
int fontsize = Convert.ToInt32(textBoxSize.Text);
int size = (int)(fontsize * 1.0 / 3 * 4 * 1.3);
Bitmap bmp = new Bitmap(size, size);
Graphics g = Graphics.FromImage(bmp);
//绘图
g.DrawString(textBoxText.Text, new Font(comboBoxFont.SelectedItem.ToString(), fontsize, FontStyle.Regular), Brushes.Black, 0, 0);
//读像素拼接字符
for (int i = 0; i < size; i++)
{
text += textBoxBack.Text;
for (int j = 0; j < size; j++)
{
Color c = bmp.GetPixel(j, i);
if (c.A == 0)
{
text += textBoxBack.Text;
}
else
{
text += textBoxFore.Text;
}
}
//换行
text += textBoxBack.Text;
text += "\r\n";
}
textBoxAnswer.Text = text;
//根据输出字号改变显示大小
if (size > 90)
{
textBoxAnswer.Font = new Font("宋体", 3f);
}
else if (size > 60)
{
textBoxAnswer.Font = new Font("宋体", 6f);
}
else if (size > 30)
{
textBoxAnswer.Font = new Font("宋体", 8f);
}
else
{
textBoxAnswer.Font = new Font("宋体", 11f);
}
}
catch
{
MessageBox.Show( "发生错误,请检查输入!","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 默认输入
/// </summary>
private void DefaultType()
{
int fontindex = 0;
foreach (string fontname in comboBoxFont.Items)
{
if (fontname == "宋体")
{
break;
}
fontindex++;
}
comboBoxFont.SelectedIndex = fontindex;
textBoxText.Text = "烧";
textBoxFore.Text = "F";
textBoxBack.Text = " ";
textBoxSize.Text = "9";
}
/// <summary>
/// 清空输入
/// </summary>
private void ClearType()
{
int fontindex = 0;
foreach (string fontname in comboBoxFont.Items)
{
if (fontname == "宋体")
{
break;
}
fontindex++;
}
comboBoxFont.SelectedIndex = fontindex;
textBoxText.Text = "";
textBoxFore.Text = "";
textBoxBack.Text = "";
textBoxSize.Text = "9";
}
/// <summary>
/// 按钮·清空输入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClear_Click(object sender, EventArgs e)
{
ClearType();
}
/// <summary>
/// 按钮·默认输入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDefault_Click(object sender, EventArgs e)
{
DefaultType();
}
/// <summary>
/// 按钮·转换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonOK_Click(object sender, EventArgs e)
{
ConvertType();
}
/// <summary>
/// 字号·限制输入数字范围
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxSize_TextChanged(object sender, EventArgs e)
{
int num = Convert.ToInt32(textBoxSize.Text);
if (num < MinFontSize) textBoxSize.Text = MinFontSize.ToString();
if (num > MaxFontSize) textBoxSize.Text = MaxFontSize.ToString();
}
/// <summary>
/// 字号·限制输入数字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
}
}