1

I have a led display i can send a byte array, one bit stands for an led. The display have 9216 leds. The byte array is 1152 bytes long (96 x 96 / 8). The first 12 bytes represent the top line, the next 12 bytes the second line,... I want work with the System.Drawing.Bitmap for drawing and send this to the display.

How can I easily convert the pixel information into this format?

var bmp = new Bitmap(96, 96);
using (var g = Graphics.FromImage(bmp))
{
    g.Clear(Color.White);
    var p = new Pen(Color.Black);
    var p1 = new Point(1, 0);
    var p2 = new Point(0, 0);
    g.DrawLine(p, p1, p2);
}

var imageBytes = Convert(bmp);

Example of a converter implementation (problems with the bits)

public static byte[] Convert(Bitmap bmp)
{
    var size = bmp.Width * bmp.Height / 8;
    var buffer = new byte[size];

    var i = 0;
    for (var y = 0; y < bmp.Height; y++)
    {
        for (var x = 0; x < bmp.Width; x++)
        {
            var color = bmp.GetPixel(x, y);
            if (color.B != 255 || color.G != 255|| color.R != 255)
            {
                var pos = i / 8;
                var bitInByteIndex = 1;

                buffer[pos] = (byte)(1 << bitInByteIndex);
            }
            i++;
        }
    }

    return buffer;
}
live2
  • 3,771
  • 2
  • 37
  • 46
  • 5
    that looks already pretty good. can you elaborate a bit more on "problems with the bits" so we know where to look? It might be as easy as `buffer[pos] |= (byte)(1 << bitInByteIndex);` so multiple bits can be set per byte. – Cee McSharpface Jan 09 '18 at 17:32
  • and `pos` is ok; `bitInByteIndex` should have to be x%8 (modulo) then. optionally reverse bytes depending on hardware's LSB/MSB. – Cee McSharpface Jan 09 '18 at 17:40
  • You probably want : for (var x = 0; x < bmp.Width; x += size)where size can be either 1 or 2 and an option for reversing bytes. – jdweng Jan 09 '18 at 18:03

1 Answers1

0

@Thank you @dlatikay!

The only question I have which one is LSB and MSB

LSB?

buffer[pos] |= (byte)(1 << (7 - bitInByteIndex));

MSB?

buffer[pos] |= (byte)(1 << bitInByteIndex);

Converter work

public static byte[] Convert(Bitmap bmp)
{
    var size = bmp.Width * bmp.Height / 8;
    var buffer = new byte[size];

    var i = 0;
    for (var y = 0; y < bmp.Height; y++)
    {
        for (var x = 0; x < bmp.Width; x++)
        {
            var color = bmp.GetPixel(x, y);
            if (color.B != 255 || color.G != 255|| color.R != 255)
            {
                var pos = i / 8;
                var bitInByteIndex = x % 8;

                buffer[pos] |= (byte)(1 << (7 - bitInByteIndex));
            }
            i++;
        }
    }

    return buffer;
}
Community
  • 1
  • 1
live2
  • 3,771
  • 2
  • 37
  • 46
  • 1
    shift operators have [higher precedence](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/) than addition/subtraction, so it should be `(byte)(1 << (7 - bitInByteIndex));` what you wrote unter "MSB" means that the most significant bit will correspond to the right pixel of the bitmap. IMO the other variant is more likely. As you don't risk damage by swapping, you can just as well test both variants. – Cee McSharpface Jan 10 '18 at 13:55