日期:2014-05-18  浏览次数:21444 次

用C#实现一个字模点阵提取程序
本文源代码位于:http://download.csdn.net/detail/caozhy/4401532

程序比较简单,已经给出了主要源代码,十分希望对程序感兴趣的同学自己动手编译调试。实在懒得动脑动手的再去下载。故而设置了10分下载分。

上大学的时候搞单片机实验,大家都知道要提取字库。

很多人还在研究UCDOS下的那些点阵文件,其实使用C#,可以很方便地写一个程序提取Windows字体。

优点是,代码实现起来非常容易。并且可以借助Windows上庞大的TTF字体资源,实现各种各样的字体,不再局限UCDOS中几种有限的字体。还可以实现很多特殊效果,因为Windows的字体就是绘图嘛。

以前用VB写过一个,写了好久才写成。最近有人问,给了他思路,他却还是搞不定,我就很郁闷了,这个很难么?

自己写了下,几行代码就出来了。主要是LINQ实在太简洁了,没办法。

代码如下:

C# code
private void button3_Click(object sender, EventArgs e)
{
    //显示按钮
    var data = textBox2.Text.Select(x => x == '1').Concat(Enumerable.Repeat(false, 256))
        .Take(256).ToArray();
    Graphics g = pictureBox1.CreateGraphics();

    for (int i = 0; i < 16; i++)
    {
        for (int j = 0; j < 16; j++)
        {
            Brush brush = data[j * 16 + i] ? Brushes.Blue : Brushes.White;
            g.FillRectangle(brush, new Rectangle() { X = i * 16, Y = j * 16, Width = 16, Height = 16 });
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
    //字体设置
    FontDialog fd = new FontDialog() { Font = textBox1.Font, FontMustExist = true };
    if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        textBox1.Font = fd.Font;
    }
}

private void button2_Click(object sender, EventArgs e)
{
    //产生字模
    Bitmap bmp = new Bitmap(16, 16);
    Graphics g = Graphics.FromImage(bmp);
    g.FillRectangle(Brushes.White, new Rectangle() { X = 0, Y = 0, Height = 16, Width = 16 });
    g.DrawString(textBox1.Text, textBox1.Font, Brushes.Black, new PointF() { X = Convert.ToSingle(domainUpDown1.Text), Y = Convert.ToSingle(domainUpDown2.Text) });
    textBox2.Text = string.Join("", Enumerable.Range(0, 256).Select(a => new { x = a % 16, y = a / 16 })
        .Select(x => bmp.GetPixel(x.x, x.y).GetBrightness() > 0.5f ? "0" : "1"));
    button3.PerformClick();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    //输入文字的文本框
    textBox1.Text = textBox1.Text.Length > 0 ? textBox1.Text.Substring(textBox1.Text.Length - 1) : "";
    textBox1.SelectionStart = textBox1.Text.Length;
}

private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
{
    //微调,用来设置偏移
    button2.PerformClick();
}

private void domainUpDown2_SelectedItemChanged(object sender, EventArgs e)
{
    button2.PerformClick();
}


运行图

Windows 7的字体效果更好。


------解决方案--------------------
ImageData[
 Binarize[Rasterize[
Style["啊", FontSize -> 12, FontFamily -> "Tahoma"]]]]



{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 0, 0, 0, 
1, 0, 0, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1}, {0, 1, 0, 1, 
0, 1, 0, 0, 0, 0, 1, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}, {0, 
1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 
1}, {0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 1, 0, 1, 0, 1, 
1, 0, 1, 1}, {0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 1, 0, 
1, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1}}
------解决方案--------------------
应该有人和我一样看不懂linq,所以我复刻了一个非linq的。。



来代码,为了对比,原来的linq在注释里,下面是代替的方法。

C# code
// 修改字体
private void button1_Click(object sender, EventArgs e)
{
    if (fontDialog1.ShowDialog() == DialogResult.OK)
        textBox1.Font = fontDialog1.Font;
}

// 生成字模
private void button2_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox1.Text))
        return;

    Bitmap bmp = new Bitmap(1