Main Site Documentation

How to convert a BMP to byte array?


#1

I need to display two numbers together on a single 8x8 LED matrix, this one specifically:
http://www.ghielectronics.com/catalog/product/370

I noticed there are some drivers out there for single numbers, but not for double numbers. So I went ahead and generated a bunch of BMP files from 00 to 99. Now I’m trying to load them from my SD card and display them on the LED Matrix.

Here are all the images:

Here is a single example image:

Here is the function I’ve been working on, it’s not working - so I’m seeking some help (perhaps I need to rethink how to load these images)??

        private byte[] GetNumberBitmap(int number)
        {
            if (number < 0 || number > 100)
                throw new Exception("Only numbers between 0 and 100 are supported.");
            if (!numbers.Contains(number.ToString()))
            {
                var leadingZero = (number < 10) ? "0" : string.Empty;
                var fullNumber = leadingZero + number;
                if (!sdCard.IsCardMounted)
                    sdCard.MountSDCard();
                var rootDir = sdCard.GetStorageDevice().RootDirectory;
                var bitmap = new byte[64];
                using(var fs = new FileStream(rootDir + @ "\CharacterBitmaps\Number" + leadingZero + number + ".bmp", FileMode.Open))
                {
                    var buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, int.Parse(fs.Length.ToString()));
                    
                    // Note: crashing here!
                    var bmp = new Bitmap(buffer, Bitmap.BitmapImageType.Bmp);
                    bitmap = bmp.GetBitmap();
                }
                numbers.Put(number.ToString(), bitmap);
            }
            return (byte[]) numbers.Get(number.ToString());
        }

#2

Exactly just use one byte per row. 8 bytes total for the whole matrix per number.


#3

There is a bunch of metadata in a BMP file, so it’s not clear how to parse out just the bits i need to make the 64 bit array. I ended up just hard coding the numbers I needed. Here is the solution if anyone else needs double numbers (like for a clock):


    public class DoubleNumberBitmapProvider : IBitmapProvider
    {
        public static byte[] Number00 = new byte[] { 0xee, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xee, 0x00 };
        public static byte[] Number01 = new byte[] {0xe4, 0xac, 0xa4, 0xa4, 0xa4, 0xa4, 0xee, 0x00};
        public static byte[] Number02 = new byte[] {0xee, 0xa2, 0xa2, 0xae, 0xa8, 0xa8, 0xee, 0x00};
        public static byte[] Number03 = new byte[] {0xee, 0xa2, 0xa2, 0xae, 0xa2, 0xa2, 0xee, 0x00};
        public static byte[] Number04 = new byte[] {0xea, 0xaa, 0xaa, 0xae, 0xa2, 0xa2, 0xe2, 0x00};
        public static byte[] Number05 = new byte[] {0xee, 0xa8, 0xa8, 0xae, 0xa2, 0xa2, 0xee, 0x00};
        public static byte[] Number06 = new byte[] {0xee, 0xa8, 0xa8, 0xae, 0xaa, 0xaa, 0xee, 0x00};
        public static byte[] Number07 = new byte[] {0xee, 0xa2, 0xa2, 0xa2, 0xa2, 0xa2, 0xe2, 0x00};
        public static byte[] Number08 = new byte[] {0xee, 0xaa, 0xaa, 0xae, 0xaa, 0xaa, 0xee, 0x00};
        public static byte[] Number09 = new byte[] {0xee, 0xaa, 0xaa, 0xae, 0xa2, 0xa2, 0xee, 0x00};
        public static byte[] Number10 = new byte[] {0x4e, 0xca, 0x4a, 0x4a, 0x4a, 0x4a, 0xee, 0x00};
        public static byte[] Number11 = new byte[] {0x44, 0xcc, 0x44, 0x44, 0x44, 0x44, 0xee, 0x00};
        public static byte[] Number12 = new byte[] {0x4e, 0xc2, 0x42, 0x4e, 0x48, 0x48, 0xee, 0x00};
        public static byte[] Number13 = new byte[] {0x4e, 0xc2, 0x42, 0x4e, 0x42, 0x42, 0xee, 0x00};
        public static byte[] Number14 = new byte[] {0x4a, 0xca, 0x4a, 0x4e, 0x42, 0x42, 0xe2, 0x00};
        public static byte[] Number15 = new byte[] {0x4e, 0xc8, 0x48, 0x4e, 0x42, 0x42, 0xee, 0x00};
        public static byte[] Number16 = new byte[] {0x4e, 0xc8, 0x48, 0x4e, 0x4a, 0x4a, 0xee, 0x00};
        public static byte[] Number17 = new byte[] {0x4e, 0xc2, 0x42, 0x42, 0x42, 0x42, 0xe2, 0x00};
        public static byte[] Number18 = new byte[] {0x4e, 0xca, 0x4a, 0x4e, 0x4a, 0x4a, 0xee, 0x00};
        public static byte[] Number19 = new byte[] {0x4e, 0xca, 0x4a, 0x4e, 0x42, 0x42, 0xee, 0x00};
        public static byte[] Number20 = new byte[] {0x4e, 0xc2, 0x42, 0x42, 0x42, 0x42, 0xe2, 0x00};
        public static byte[] Number21 = new byte[] {0xe4, 0x2c, 0x24, 0xe4, 0x84, 0x84, 0xee, 0x00};
        public static byte[] Number22 = new byte[] {0xee, 0x22, 0x22, 0xee, 0x88, 0x88, 0xee, 0x00};
        public static byte[] Number23 = new byte[] {0xee, 0x22, 0x22, 0xee, 0x82, 0x82, 0xee, 0x00};
        public static byte[] Number24 = new byte[] {0xea, 0x2a, 0x2a, 0xee, 0x82, 0x82, 0xe2, 0x00};
        public static byte[] Number25 = new byte[] {0xee, 0x28, 0x28, 0xee, 0x82, 0x82, 0xee, 0x00};
        public static byte[] Number26 = new byte[] {0xee, 0x28, 0x28, 0xee, 0x8a, 0x8a, 0xee, 0x00};
        public static byte[] Number27 = new byte[] {0xee, 0x22, 0x22, 0xe2, 0x82, 0x82, 0xe2, 0x00};
        public static byte[] Number28 = new byte[] {0xee, 0x2a, 0x2a, 0xee, 0x8a, 0x8a, 0xee, 0x00};
        public static byte[] Number29 = new byte[] {0xee, 0x2a, 0x2a, 0xee, 0x82, 0x82, 0xee, 0x00};
        public static byte[] Number30 = new byte[] {0xee, 0x2a, 0x2a, 0xea, 0x2a, 0x2a, 0xee, 0x00};
        public static byte[] Number31 = new byte[] {0xe4, 0x2c, 0x24, 0xe4, 0x24, 0x24, 0xee, 0x00};
        public static byte[] Number32 = new byte[] {0xee, 0x22, 0x22, 0xee, 0x28, 0x28, 0xee, 0x00};
        public static byte[] Number33 = new byte[] {0xee, 0x22, 0x22, 0xee, 0x22, 0x22, 0xee, 0x00};
        public static byte[] Number34 = new byte[] {0xea, 0x2a, 0x2a, 0xee, 0x22, 0x22, 0xe2, 0x00};
        public static byte[] Number35 = new byte[] {0xee, 0x28, 0x28, 0xee, 0x22, 0x22, 0xee, 0x00};
        public static byte[] Number36 = new byte[] {0xee, 0x28, 0x28, 0xee, 0x2a, 0x2a, 0xee, 0x00};
        public static byte[] Number37 = new byte[] {0xee, 0x22, 0x22, 0xe2, 0x22, 0x22, 0xe2, 0x00};
        public static byte[] Number38 = new byte[] {0xee, 0x2a, 0x2a, 0xee, 0x2a, 0x2a, 0xee, 0x00};
        public static byte[] Number39 = new byte[] {0xee, 0x2a, 0x2a, 0xee, 0x22, 0x22, 0xee, 0x00};
        public static byte[] Number40 = new byte[] {0xae, 0xaa, 0xaa, 0xea, 0x2a, 0x2a, 0x2e, 0x00};
        public static byte[] Number41 = new byte[] {0xa4, 0xac, 0xa4, 0xe4, 0x24, 0x24, 0x2e, 0x00};
        public static byte[] Number42 = new byte[] {0xae, 0xa2, 0xa2, 0xee, 0x28, 0x28, 0x2e, 0x00};
        public static byte[] Number43 = new byte[] {0xae, 0xa2, 0xa2, 0xee, 0x22, 0x22, 0x2e, 0x00};
        public static byte[] Number44 = new byte[] {0xaa, 0xaa, 0xaa, 0xee, 0x22, 0x22, 0x22, 0x00};
        public static byte[] Number45 = new byte[] {0xae, 0xa8, 0xa8, 0xee, 0x22, 0x22, 0x2e, 0x00};
        public static byte[] Number46 = new byte[] {0xae, 0xa8, 0xa8, 0xee, 0x2a, 0x2a, 0x2e, 0x00};
        public static byte[] Number47 = new byte[] {0xae, 0xa2, 0xa2, 0xe2, 0x22, 0x22, 0x22, 0x00};
        public static byte[] Number48 = new byte[] {0xae, 0xaa, 0xaa, 0xee, 0x2a, 0x2a, 0x2e, 0x00};
        public static byte[] Number49 = new byte[] {0xae, 0xaa, 0xaa, 0xee, 0x22, 0x22, 0x2e, 0x00};
        public static byte[] Number50 = new byte[] {0xee, 0x8a, 0x8a, 0xea, 0x2a, 0x2a, 0xee, 0x00};
        public static byte[] Number51 = new byte[] {0xe4, 0x8c, 0x84, 0xe4, 0x24, 0x24, 0xee, 0x00};
        public static byte[] Number52 = new byte[] {0xee, 0x82, 0x82, 0xee, 0x28, 0x28, 0xee, 0x00};
        public static byte[] Number53 = new byte[] {0xee, 0x82, 0x82, 0xee, 0x22, 0x22, 0xee, 0x00};
        public static byte[] Number54 = new byte[] {0xea, 0x8a, 0x8a, 0xee, 0x22, 0x22, 0xe2, 0x00};
        public static byte[] Number55 = new byte[] {0xee, 0x88, 0x88, 0xee, 0x22, 0x22, 0xee, 0x00};
        public static byte[] Number56 = new byte[] {0xee, 0x88, 0x88, 0xee, 0x2a, 0x2a, 0xee, 0x00};
        public static byte[] Number57 = new byte[] {0xee, 0x82, 0x82, 0xe2, 0x22, 0x22, 0xe2, 0x00};
        public static byte[] Number58 = new byte[] {0xee, 0x8a, 0x8a, 0xee, 0x2a, 0x2a, 0xee, 0x00};
        public static byte[] Number59 = new byte[] {0xee, 0x8a, 0x8a, 0xee, 0x22, 0x22, 0xee, 0x00};

        #region IBitmapProvider Members

        public byte[] GetBitmap(string key)
        {
            var number = int.Parse(key);

            if (number < 0 || number > 60)
                throw new ArgumentException("Number must be between 0 and 60.", "key");

            if (key.Length == 1)
                key = "0" + key;

            return GetType().GetField("Number" + key).GetValue(this) as byte[];
        }

        #endregion
    }


#4

You can save space by encoding only 10 single digits. And draw two digits by doing some simple math/bit manipulation.


#5

if you want to convert BMP to byte array . you need to look at the codes here: http://www.rasteredge.com/how-to/vb-net-imaging/convert-image-to-byte/