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