使用C#使用ESC / POS命令在EPSON LX-300 + II上打印图像

时间:2018-09-10 08:17:30

标签: c# asp.net matrix printing escpos

我正在尝试使用ESC / POS命令在EPSON LX-300 + II打印机上打印图像。但是打印出来不是我所期望的。它正在打印一些空白行和垃圾字符。下面是我尝试过的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{



class Program
{
    static void Main(string[] args)
    {
        Print("EPSON LX-300+II ESC/P", GetLogo());
        Console.ReadLine();
    }
    public static byte[] GetLogo()
    {
        if (!File.Exists(@"C:\Users\124753  \Desktop\ClientUtility\BitMap.bmp"))
            return null;
        BitmapData data = GetBitmapData(@"C:\Users\124753 \Desktop\ClientUtility\BitMap.bmp");
        BitArray dots = data.Dots;
        byte[] width = BitConverter.GetBytes(data.Width);

        int offset = 0;
        MemoryStream stream = new MemoryStream();
        BinaryWriter bw = new BinaryWriter(stream);

        bw.Write((char)0x1B);
        bw.Write('@');

        bw.Write((char)0x1B);
        bw.Write('3');
        bw.Write((byte)24);

        while (offset < data.Height)
        {
            bw.Write((char)0x1B);
            bw.Write('*');         // bit-image mode
            bw.Write((byte)33);    // 24-dot double-density
            bw.Write(width[0]);  // width low byte
            bw.Write(width[1]);  // width high byte

            for (int x = 0; x < data.Width; ++x)
            {
                for (int k = 0; k < 3; ++k)
                {
                    byte slice = 0;
                    for (int b = 0; b < 8; ++b)
                    {
                        int y = (((offset / 8) + k) * 8) + b;
                        // Calculate the location of the pixel we want in the bit array.
                        // It'll be at (y * width) + x.
                        int i = (y * data.Width) + x;

                        // If the image is shorter than 24 dots, pad with zero.
                        bool v = false;
                        if (i < dots.Length)
                        {
                            v = dots[i];
                        }
                        slice |= (byte)((v ? 1 : 0) << (7 - b));
                    }

                    bw.Write(slice);
                }
            }
            offset += 24;
            bw.Write((char)0x0A);
        }
        // Restore the line spacing to the default of 30 dots.
        bw.Write((char)0x1B);
        bw.Write('3');
        bw.Write((byte)30);

        bw.Flush();
        byte[] bytes = stream.ToArray();
        return bytes;
    }

    public static BitmapData GetBitmapData(string bmpFileName)
    {
        using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
        {
            var threshold = 127;
            var index = 0;
            double multiplier = 570; // this depends on your printer model.  for Beiyang you should use 1000
            double scale = (double)(multiplier / (double)bitmap.Width);
            int xheight = (int)(bitmap.Height * scale);
            int xwidth = (int)(bitmap.Width * scale);
            var dimensions = xwidth * xheight;
            var dots = new BitArray(dimensions);

            for (var y = 0; y < xheight; y++)
            {
                for (var x = 0; x < xwidth; x++)
                {
                    var _x = (int)(x / scale);
                    var _y = (int)(y / scale);
                    var color = bitmap.GetPixel(_x, _y);
                    var luminance = (int)(color.R * 0.3 + color.G * 0.59 +  color.B * 0.11);
                    dots[index] = (luminance < threshold);
                    index++;
                }
            }

            return new BitmapData()
            {
                Dots = dots,
                Height = (int)(bitmap.Height * scale),
                Width = (int)(bitmap.Width * scale)
            };
        }
    }
     private static void Print(string printerName, byte[] document)

    {

        NativeMethods.DOC_INFO_1 documentInfo;

        IntPtr printerHandle;



        documentInfo = new NativeMethods.DOC_INFO_1();

        documentInfo.pDataType = "RAW";

        documentInfo.pDocName = "Bit Image Test";



        printerHandle = new IntPtr(0);



        if (NativeMethods.OpenPrinter(printerName.Normalize(), out  printerHandle, IntPtr.Zero))

        {

            if (NativeMethods.StartDocPrinter(printerHandle, 1, documentInfo))

            {

                int bytesWritten;

                byte[] managedData;

                IntPtr unmanagedData;



                managedData = document;

                unmanagedData = Marshal.AllocCoTaskMem(managedData.Length);

                Marshal.Copy(managedData, 0, unmanagedData, managedData.Length);



                if (NativeMethods.StartPagePrinter(printerHandle))

                {

                    NativeMethods.WritePrinter(

                        printerHandle,

                        unmanagedData,

                        managedData.Length,

                        out bytesWritten);

                    NativeMethods.EndPagePrinter(printerHandle);

                }

                else

                {

                    throw new Win32Exception();

                }



                Marshal.FreeCoTaskMem(unmanagedData);



                NativeMethods.EndDocPrinter(printerHandle);

            }

            else

            {

                throw new Win32Exception();

            }



            NativeMethods.ClosePrinter(printerHandle);

        }

        else

        {

            throw new Win32Exception();

        }

    }
}
public class BitmapData
{
    public BitArray Dots
    {
        get;
        set;
    }

    public int Height
    {
        get;
        set;
    }

    public int Width
    {
        get;
        set;
    }
}

}

我从Web上获取了以上代码,以在热敏打印机中打印图像。但是我相信它也可以在提到的打印机上工作。有人可以帮忙吗?谢谢

0 个答案:

没有答案