小像素字体

时间:2014-06-07 01:51:09

标签: c# fonts bitmap vector-graphics led

我试图在显示器上显示像素,为我在一组LED上显示时做准备。 我这样做是使用字体绘制到位图上,然后读取位图,无论像素是更亮还是更暗,并将其放入二维的布尔数组中。 然后将此数组传递到我的特定于接口的显示方法中以进行显示。 由于此设置仅为6像素高,因此我使用了www.dafont.com/6px.font为六像素分辨率制作的字体。 目前这个系统没有很好地显示 - 有些字符不是真正可读的。

这甚至是正确的方法吗?我该怎么办? 谢谢!

像素地图文件:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;

namespace Phidgets
{
    public class StringDrawer
    {
        internal int height, width;
        internal bool[][] mapData;
        GraphicalInterface MyGraphicalInterface;
        Bitmap canvas;
        int MaxPixelLength;
        Graphics canvasGraphics;
        Brush brush = new SolidBrush(Color.Black);
        Font font;
        public StringDrawer(int width, int height, GraphicalInterface myInterface)
        {
            InitFont();
            this.height = height;
            this.width = width;
            MaxPixelLength = 2048 + width;//2048 pixels, plus the amount of empty pixels at the end.
            mapData = new bool[height][];
            for (int i = 0; i < height; i++)
            {
                mapData[i] = new bool[width];
            }
            MyGraphicalInterface = myInterface;
            canvas = new Bitmap(MaxPixelLength, height);
            canvasGraphics = Graphics.FromImage(canvas);
        }
        void DrawPoint(int x, int y)
        {
            mapData[x][y] = true;
        }
        void Clear()
        {
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    mapData[i][j] = false;
                }
            }
        }
        public void DrawString(String input)
        {
            input = input.ToUpper();
            Clear();
            PaintCanvas(input);
            UpdateDataFromCanvas();
            MyGraphicalInterface.Update(mapData);
        }
        public void UpdateDataFromCanvas()
        {
            for (int y = 0; y < mapData.Count(); y++)
            {
                for (int x = 0; x < mapData[0].Length; x++)
                {
                    bool b = canvas.GetPixel(x, y).R < 128;//r g or b is equivalent here, half way between light and dark.
                    mapData[y][x] = b;
                }
            }
        }
        Bitmap GetCurrentFrame(int startX)
        {
            return null;
        }
        void PaintCanvas(String input)
        {
            canvasGraphics.Clear(Color.White);
            canvasGraphics.DrawString(input, regFont, solidBrush, 0, 0);
        }
        SolidBrush solidBrush;
        Font regFont;
        void InitFont()
        {
            System.Drawing.Text.PrivateFontCollection privateFontCollection = new System.Drawing.Text.PrivateFontCollection();
            privateFontCollection.AddFontFile("../../6px-Normal.ttf");
            solidBrush = new SolidBrush(Color.Black);
            FontFamily[] fontFamilies = privateFontCollection.Families;
            string familyName = fontFamilies[0].Name;
            regFont = new Font(familyName, 6,FontStyle.Regular, GraphicsUnit.Pixel);
        }
    }
    public abstract class GraphicalInterface
    {
        public abstract void Update(bool[][] mapData);
    }
    public class ScreenInterface : GraphicalInterface
    {
        //int xStart = 350;
        //int yStart = 23;
        int xStart, yStart;
        Graphics g;
        Brush brush = new SolidBrush(Color.Black);
        public ScreenInterface(int xStart, int yStart, Form1 f)
        {
            g = f.CreateGraphics();
            this.xStart = xStart;
            this.yStart = yStart;
        }
        public override void Update(bool[][] mapData)
        {
            const int scale = 4;
            for (int y = 0; y < mapData.Count(); y++)
            {
                for (int x = 0; x < mapData[0].Length; x++)
                {
                    if (mapData[y][x])
                        g.FillRectangle(brush, xStart + (x * scale), yStart + (y * scale), scale, scale);
                }
            }
        }
    }
    public class LedInterface : GraphicalInterface
    {
        public override void Update(bool[][] mapData)
        {
            for (int y = 0; y < mapData.Count(); y++)
            {
                for (int x = 0; x < mapData[0].Length; x++)
                {
                    //not implemented
                }
            }
        }
    }
}

Form1文件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Phidgets
{
    public partial class Form1 : Form
    {
        StringDrawer testDrawer, LedDrawer;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            const int width = 24;
            const int height = 6;
            ScreenInterface screen = new ScreenInterface(450, 23, this);
            testDrawer = new StringDrawer(width, height, screen);
            LedDrawer = new StringDrawer(width, height, new LedInterface());
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            testDrawer.DrawString("a b c");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Thanks to defont.com/6px for the font used, and as always, much wow to stackoverflow.");
        }
    }
}

非常感谢任何帮助 - 谢谢!

1 个答案:

答案 0 :(得分:0)

找出问题 - 字体甚至没有分配!

我打电话的时候:     regFont = new Font(familyName,6,FontStyle.Regular,GraphicsUnit.Pixel);

它没有改变!就像,如果你在调试器中经历过,那么在赋值后值并没有改变。太奇怪了。但是当我改变InitFont方法时说:

    void InitFont()
    {
        System.Drawing.Text.PrivateFontCollection privateFontCollection =
           new System.Drawing.Text.PrivateFontCollection();
        privateFontCollection.AddFontFile("../../6px-Normal.ttf");
        solidBrush = new SolidBrush(Color.Black);
        FontFamily[] fontFamilies = privateFontCollection.Families;
        regFont = new Font(fontFamilies[0], 6);
    }

任务奏效了。 所以有点反高潮,但感谢你们的帮助。