views:

234

answers:

2

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.

+2  A: 

You're looking for the LockBits method.

SLaks
I used your method but there is a follow up question with a problem I encountered. http://stackoverflow.com/questions/2595393/
Scott Chamberlain
A: 

If you just need to convert your bitmap into a byte array, try using a MemoryStream:

Check out this link: C# Image to Byte Array and Byte Array to Image Converter Class

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}
JYelton