C#关于字模提取一个软件疑问

wswhjxx 2018-08-14 10:03:52
程序参照了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一样,如下图


求大神指点
...全文
264 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wswhjxx 2018-08-17
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
下面两个微调框就是给你调的。

请教大神,更改字体时,为什么显示就不对了呢

上图是楷体小四
wswhjxx 2018-08-14
  • 打赏
  • 举报
回复
第一次发帖求助,没有积分,还往大侠指点下
wswhjxx 2018-08-14
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
下面两个微调框就是给你调的。

想问下是什么造成的这个偏移,现在是一个字符的偏移,后面我准备写多个字符,如果每个都偏移的话,就不知道该怎么处理了,还请大侠指点下,谢谢。
threenewbee 2018-08-14
  • 打赏
  • 举报
回复
下面两个微调框就是给你调的。

493

社区成员

发帖
与我相关
我的任务
社区描述
硬件使用 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧