Convert a image to a monochrome byte array

Posted by Scott Chamberlain on Stack Overflow See other posts from Stack Overflow or by Scott Chamberlain
Published on 2010-04-07T15:32:29Z Indexed on 2010/04/07 15:33 UTC
Read the original article Hit count: 217

I am writing a library to interface C# with the EPL2 printer language. One feature I would like to try to implement is printing images, the specification doc says

p1 = Width of graphic Width of graphic in bytes. Eight (8) dots = one (1) byte of data.

p2 = Length of graphic Length of graphic in dots (or print lines)

Data = Raw binary data without graphic file formatting. Data must be in bytes. Multiply the width in bytes (p1) by the number of print lines (p2) for the total amount of graphic data. The printer automatically calculates the exact size of the data block based upon this formula.

I plan on my source image being a 1 bit per pixel bmp file, already scaled to size. I just don't know how to get it from that format in to a byte[] for me to send off to the printer. I tried ImageConverter.ConvertTo(Object, Type) it succeeds but the array it outputs is not the correct size and the documentation is very lacking on how the output is formatted.

My current test code.

Bitmap i = (Bitmap)Bitmap.FromFile("test.bmp");
ImageConverter ic = new ImageConverter();
byte[] b = (byte[])ic.ConvertTo(i, typeof(byte[]));

Any help is greatly appreciated even if it is in a totally different direction.

© Stack Overflow or respective owner

Related posts about c#

Related posts about image-manipulation