0

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();
}
3
  • Please edit and reformat the code in your question. The editor shows a preview of it when posting, please make sure it looks proper. Commented Oct 20, 2016 at 6:27
  • Sorry for that. Please check the edited post. Thanks Commented Oct 20, 2016 at 6:35
  • Did you figure out what the problem was? Commented Oct 21, 2016 at 9:20

1 Answer 1

1

Refactored your solution a bit. Was quiet hard to figure out what was going on. Tested my solution, on a small bitmap, and it seems that my solution is doing the correct thing. At least on your requirement.

Bitmap bitmap = (Bitmap)this.PictureBoxOriginalBitmap.Image;

var width = bitmap.Width;
var height = bitmap.Height;

var shiftEnumerable = Enumerable.Range(0, 8).Reverse(); // create an array from (7..0) MSB as the first

var dataList = new List<byte>(); // Create a list instead of fiddeling with array indexes
for (int rowIndex = 7; rowIndex < height; rowIndex=rowIndex+8 ) // increment 8 pixels each iteration on y-axis. If picture is less than 8 pixels, convertion will not happen.
{
   for (int columnIndex = 0; columnIndex < width; columnIndex++) // increment x-axis
   {
       // Linq which performs a bitwise OR operation. Converts the predicate to byte and shifts result x places depending on which bit needs to be set.
       // Add the accumulated OR operation to a list of bytes.
       dataList.Add(shiftEnumerable.Aggregate<int, byte>(
                    0,
                    (accumulate, current) => (byte)(accumulate | (byte)(Convert.ToByte(bitmap.GetPixel(columnIndex, rowIndex - current).R == 255) << current))));
    }
}
// Convert List to array.
var data = dataList.ToArray();

Did this because it was a fun exercise for me. Please let me know if you can use it :-)

EDIT:

idx:  New  | Old
-----------------------
[0]:  170  | 170
[1]:  255  | 0
[2]:  127  | 1
[3]:  63   | 3
[4]:  31   | 7
[5]:  15   | 15
[6]:  7    | 31
[7]:  3    | 63
[8]:  129  | 126
[9]:  192  | 252
[10]: 224  | 248
[11]: 240  | 240
[12]: 248  | 224
[13]: 252  | 192
[14]: 254  | 128
[15]: 255  | 0

Tried to compare the output of the the data[] array, and as you can see. Our results are quite different.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.