程序参照了caozhy的贴子代码,原文链接https://bbs.csdn.net/topics/390114478
在此基础上略做修改,生成显示为16进制,修改代码如下:
//生成字模
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
return;
Bitmap bmp = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
pictureBox1.Refresh();
g.DrawString(textBox1.Text, textBox1.Font, Brushes.Black, new PointF() { X = Convert.ToSingle(domainUpDown1.Text), Y = Convert.ToSingle(domainUpDown2.Text) });
StringBuilder sb = new StringBuilder();
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 16; x++)
{
if (bmp.GetPixel(x, y).GetBrightness() > 0.5f)
sb.Append("0");
else
sb.Append("1");
}
}
string temp = sb.ToString();
//textBox2.Text = temp;
bool[] data = new bool[256];
for (int i = 0; i < data.Count(); i++)
{
data[i] = temp[i] == '1';
}
Graphics g1 = pictureBox1.CreateGraphics();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
Brush brush = data[j * 16 + i] ? Brushes.Red : Brushes.Transparent;
g1.FillRectangle(brush, i * 16, j * 16, 16, 16);
}
}
string[] btemp = new string[16];
string sstemp = string.Empty;
string stemp = temp;
for (int s = 0; s < 16; s++)
{
string stemp1 = stemp.Substring(0, 16);
int inttemp = Convert.ToInt32(stemp1, 2);
btemp[s] = "0x"+inttemp.ToString("x4");
sstemp += btemp[s]+",";
stemp = stemp.Remove(0, 16);
}
textBox2.Text = sstemp;
g.Dispose();
g1.Dispose();
}
效果图如下:
字体:宋体 字号:小四

和自己想象的效果有点出入,在直接生成的情况下,图像为什么会向右偏移了3个格子呢?
当字号不同时,偏移的格子也不一样,求大神指点。
下图 字体:宋体 字号:五号 ,与想象的效果向右偏移了2格

个人想做出来的效果是,生成的图片和PCtoLCD2002一样,如下图

求大神指点