I need to convert a bitmap image to a byte array and print it using thermal printer where the top pixel of each column is the MSB and the bottom pixel is the LSB. Currently, my implementation is like this but it does not print the image as expected:
Bitmap bitmap = (Bitmap)this.PictureBoxOriginalBitmap.Image;
int width, height;
width = bitmap.Width;
height = bitmap.Height;
byte[] data = new byte[height * width / 8];
uint counter = 0, index = 0;
byte[] sequence = new byte[8] { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
for (int rowIndex = 0; rowIndex < height / 8; rowIndex++)
{
for (int columnIndex = 0; columnIndex < width; columnIndex++)
{
for (int pixelColumn = rowIndex * 8; pixelColumn < (rowIndex + 1) *8; pixelColumn++)
{
data[counter] |= (byte)((bitmap.GetPixel(columnIndex, pixelColumn).R == 255) ? 0x00 : sequence[index]);
if (++index == 8)
{
counter++;
index = 0;
}
}
}
}
StringBuilder message = new StringBuilder();
byte[] buffer;
this._bitmapInfo = new BitmapInformation();
buffer = new byte[width];
for (int rowIndex = 0; rowIndex < height / 8; rowIndex++)
{
Array.Copy(data, width * rowIndex, buffer, 0, width);
message.AppendLine(Helper.byteAsStringWith0x(buffer, true).Trim());
this._bitmapInfo[rowIndex] = Helper.byteAsStringWith0x(buffer,true).Trim();
}