2014-06-07 40 views
1

我正在嘗試在顯示器上顯示像素,準備在一組LED上顯示時。 我正在使用字體繪製位圖,然後讀取位圖,無論像素是較亮還是較暗,並將其放入布爾值的二維數組中。 然後將此數組傳遞到特定於接口的顯示方法中進行顯示。 由於這套設備只有6個像素高,我使用的字體是從www.dafont.com/6px.font獲得6像素分辨率。 目前這個系統不能很好地顯示 - 有些字符不是真正可讀。小像素字體

這是甚至正確的方法嗎?我該怎麼做? 謝謝!

像素映射文件:

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."); 
     } 
    } 
} 

任何幫助深表感謝 - 感謝!

+0

如果有效,不用擔心。我想,根據你所做的事情(畫在屏幕外的位圖上)是一個好方法。它不僅可以讓您輕鬆地使用和混合不同的字體和樣式(斜體,粗體等等 - 雖然可能與6px的高度無關),但它也可以很容易地提高下一個LED顯示屏的分辨率同時保持相同的字體,如果你願意... – elgonzo

+0

歡呼你的評論,問題是,它不是很好的顯示,此刻。如果我調整'128'值可能會更好或更糟,但目前它不是真正可讀的。 – Jack

+0

答案可能是http://stackoverflow.com/search?q=[c%23]+turn+off+antialiasing帖子之間的某個地方... –

回答

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); 
    } 

分配工作。 所以有點反高潮,但謝謝你們的幫助。

相關問題