Print image in page mode using esc commands

135 Views Asked by At

Image is not printed in page mode using "GS v 0" command. Here is my code:

private static string ConvertBitmapToCommandsForMode1(Bitmap bitmap, byte[] image = null)
{
    List<byte> commands = new List<byte>();

    var e = new ESCPOS_NET.Emitters.EPSON();

    //return BitConverter.ToString(commands.ToArray()).Replace("-", "");
    // 1. Select page mode
    commands.Add(0x1B); // ESC in hex
    commands.Add(0x4C); // L in hex

    // 2. Change horizontal and vertical motion units to set normal dot units
    commands.Add(0x1D); // GS in hex
    commands.Add(0x50); // P in hex
    commands.Add(0xB4); // Decimal 180 in hex
    commands.Add(0xB4); // Decimal 180 in hex
    // the printer will move 180 dots for every inch it moves horizontally or vertically.

    // 3. Direction
    commands.Add(0x1B); // ESC
    commands.Add(0x54); // T
    commands.Add(0x00);
    // print direction 0x00 would be left to right, 0x01 would be bottom to top, 0x02 would be right to left, and 0x03 would be top to bottom.

    // 4. Select the printing area for edge data
    commands.Add(0x1B); // ESC
    commands.Add(0x57); // W
    commands.AddRange(new byte[] {
        0x14, 0x00, // 0 (xL, xH)
        0x64, 0x00, // 0 (yL, yH)
        0x64, 0x00, // 100 (dxL, dxH)
        0x64, 0x00  // 100 (dyL, dyH)
    });
    // 0x90 0x01: These two bytes define the X coordinate of the upper-left corner of the printing area. The first byte (0x90) is the low-order byte, and the second byte (0x01) is the high-order byte. To get the X coordinate in decimal, you combine these two bytes into a 16-bit value: 0x0190 in hexadecimal is 400 in decimal.
    // 0x76 0x00: These two bytes define the Y coordinate of the upper-left corner of the printing area. Here, 0x0076 in hexadecimal translates to 118 in decimal.
    // 0x68 0x01: These two bytes represent the printing area width in dots. 0x0168 in hexadecimal is 360 in decimal.
    // 0x10 0x02: Finally, these two bytes represent the printing area height in dots. 0x0210 in hexadecimal is 528 in decimal.

    commands.Add(0x0A); // Line Feed
    commands.AddRange(Encoding.ASCII.GetBytes("123eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"));
    commands.Add(0x0A); // Line Feed
    commands.Add(0x0A); // Line Feed

    // 5. Select the printing area for edge data
    commands.Add(0x1B); // ESC
    commands.Add(0x57); // W
    commands.AddRange(new byte[] {
        0x64, 0x00, // 100 (xL, xH)
        0x00, 0x00, // 0 (yL, yH)
        0xF4, 0x01, // 500  (dxL, dxH)
        0xC8, 0x01  // 200  (dyL, dyH)
    });
    
    // 6. Print Image
    commands.AddRange(e.PrintImage(image, true, true, 300));

    // 7. Print all data and cut
    // Print all
    commands.Add(0x1B);
    commands.Add(0x0C);

    // 8. Select cut mode and cut paper
    commands.Add(0x1D); // GS in hex
    commands.Add(0x56); // V in hex
    commands.Add(0x42); // Decimal 66 in hex
    commands.Add(0x50); // Decimal 80 in hex

    commands.Add(0x1B); // ESC in hex
    commands.Add(0x53); // S in hex

    return BitConverter.ToString(commands.ToArray()).Replace("-", "");
}

Result: enter image description here

I've tried using the GS v 0 command for printing the image and the ESC L command for selecting page mode. Additionally, I've attempted to use the ESC * command, but the printed image has spaces between every 8-bit segment.

0

There are 0 best solutions below