how to print images with ESC/POS commands?

Mehdi picture Mehdi · Apr 30, 2016 · Viewed 12.8k times · Source

i have an Epson-TMH6000III thermal printer and i want to print some bitmap with it by using ESC/POS commands.

but before this i want to print a very simple single line with ESC/POS printing image commands.

here's my attempt :

namespace printingImageMode
{
    class Program
    {
        static void Main(string[] args)
        {
            Bitmap bmp = new Bitmap(@"C:\Users\falamarzi\Desktop\Kyan Graphic Viewer\jTest.jpg");

            int msb = (int)(bmp.Width & 0x0000ff00) >> 8;
            int lsb = (int)(bmp.Width & 0x000000ff);

        byte msbB = Convert.ToByte(msb);
        byte lsbB = Convert.ToByte(lsb);

        byte[] enter_To_Image_Printing_Mode_Command = new byte[] { (byte)AsciiControlChars.ESC, (byte)DensityCommand.EightDot_SD, msbB, lsbB };

        byte[] imageData = new byte[lsb + msb * 256];

        for (int i = 0; i < imageData.Length; i++)
        {
            imageData[i] = 0xff;
        }

        byte[] complete_Command = new byte[enter_To_Image_Printing_Mode_Command.Length + imageData.Length];

        enter_To_Image_Printing_Mode_Command.CopyTo(complete_Command, 0);
        imageData.CopyTo(complete_Command, enter_To_Image_Printing_Mode_Command.Length);

        SerialPort sPort = new SerialPort("COM5");
        sPort.Open();

        sPort.Write(complete_Command, 0, complete_Command.Length);
    }

}

public enum AsciiControlChars : byte
{
    ESC = 0x1b,
}

    public enum DensityCommand : byte
    {
        EightDot_SD = 0x00,
        EightDot_DD = 0x01,
        TwentyFourDot_SD = 0x20,
        TwentyFourDot_DD = 0x21,
    }
}

i didn't get the result. i appreciate for any help in this.

Answer

trandi picture trandi · Nov 29, 2017

Probably too late to be useful for the initial question, but for future reference, as I've been searching myself quite a lot before finding how to send bit images to a printer using POS.

Among the several options, it seems that the easiest one is using the "ESC*0" command followed by the number of bytes (2 bytes, high and low), the actual data and then a "\n".

All the details / specs for the command are in the manual if you search for "ESC * Select bit image", but knowing that this option exists and it's relatively simple and fast is really the tricky bit...

You can also find a concrete code example, in Haskell and some more details on this post.