标准32×32字符点阵例子:
![]()
这里介绍的是利用的绘图功能动态生成字符点阵,而非查找汉字点阵库获取对应点阵的方法,故适用于任何大小要求的任意字符。
具体原理是利用位图变量将字符“显示”出来,然后根据它的每个象素的颜色来判断该点是否存在。
首先获取一个字符的位图变量
public Bitmap GetCharBMP(string str,int size)
{
StringFormat sf = new StringFormat(); // 设置格式
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Near;
Bitmap bmp = new Bitmap(size,size); // 新建位图变量
Graphics g = Graphics.FromImage(bmp);
g.DrawString(str,new Font("宋体",size*3/4),Brushes.Black,new Rectangle(0,0,size,size),sf); // 向图像变量输出字符
return bmp;
}
![]()
然后根据每一象素的颜色获取每一位置的信息,从而构造标准点阵
public void CreateCharSetFile(string filePath,int size)
{
StreamWriter sw = new StreamWriter(filePath,false,System.Text.Encoding.Default);
sw.WriteLine("char char_set_pixel [] = {"); // 开始写入字符点阵
foreach (char ch in charSet)
{
Bitmap bmp = GetCharBMP(ch.ToString(),size); // 获取待分析的字符位图
for (int i = 0;i < bmp.Height;i++)
{
byte temp = 0;
for (int j = 0;j < bmp.Width;j++)
{
if (bmp.GetPixel(j,i) == Color.FromArgb(0,0,0))// 以下几行根据点阵格式计算它的十六进制并写入
temp += (byte)Math.Pow(2,(size/4 - 1) - j % (size/4));
if (j % 8 == 7)
{
if (temp.ToString("x").Length == 2)
sw.Write("0x" + temp.ToString("x"));
else sw.Write("0x0" + temp.ToString("x"));
if (!(j == bmp.Width -1 && i == bmp.Height -1 && ch == charSet[charSet.Length - 1]))
sw.Write(",");
temp = 0;
}
}
if (i % 4 == 3)
sw.WriteLine();
}
sw.WriteLine();
}
sw.WriteLine("};");
sw.Close();
}
这里介绍的是利用的绘图功能动态生成字符点阵,而非查找汉字点阵库获取对应点阵的方法,故适用于任何大小要求的任意字符。
具体原理是利用位图变量将字符“显示”出来,然后根据它的每个象素的颜色来判断该点是否存在。
首先获取一个字符的位图变量
public Bitmap GetCharBMP(string str,int size)
{
StringFormat sf = new StringFormat(); // 设置格式
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Near;
Bitmap bmp = new Bitmap(size,size); // 新建位图变量
Graphics g = Graphics.FromImage(bmp);
g.DrawString(str,new Font("宋体",size*3/4),Brushes.Black,new Rectangle(0,0,size,size),sf); // 向图像变量输出字符
return bmp;
}
然后根据每一象素的颜色获取每一位置的信息,从而构造标准点阵
public void CreateCharSetFile(string filePath,int size)
{
StreamWriter sw = new StreamWriter(filePath,false,System.Text.Encoding.Default);
sw.WriteLine("char char_set_pixel [] = {"); // 开始写入字符点阵
foreach (char ch in charSet)
{
Bitmap bmp = GetCharBMP(ch.ToString(),size); // 获取待分析的字符位图
for (int i = 0;i < bmp.Height;i++)
{
byte temp = 0;
for (int j = 0;j < bmp.Width;j++)
{
if (bmp.GetPixel(j,i) == Color.FromArgb(0,0,0))// 以下几行根据点阵格式计算它的十六进制并写入
temp += (byte)Math.Pow(2,(size/4 - 1) - j % (size/4));
if (j % 8 == 7)
{
if (temp.ToString("x").Length == 2)
sw.Write("0x" + temp.ToString("x"));
else sw.Write("0x0" + temp.ToString("x"));
if (!(j == bmp.Width -1 && i == bmp.Height -1 && ch == charSet[charSet.Length - 1]))
sw.Write(",");
temp = 0;
}
}
if (i % 4 == 3)
sw.WriteLine();
}
sw.WriteLine();
}
sw.WriteLine("};");
sw.Close();
}
