日期:2014-05-16  浏览次数:21020 次

用C#实现一个字模点阵提取程序
本帖最后由 lisw2011 于 2012-07-02 17:01:00 编辑
本文源代码位于:http://download.csdn.net/detail/caozhy/4401532

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

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

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

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

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

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

代码如下:

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.Lengt