首页 > 编程语言 >C#-斑马打印机Helper(COM/LPT/DRV/ZPL/EPL)_适用于标签、票据、条码打印

C#-斑马打印机Helper(COM/LPT/DRV/ZPL/EPL)_适用于标签、票据、条码打印

时间:2023-04-15 18:59:43浏览次数:38  
标签:string Helper C# LPT int static new bool public

  原文:http://www.zzvips.com/article/207824.html、https://blog.csdn.net/sqzhao/article/details/52488939

1、ZebraHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace Common
{
    /// <summary>
    /// ZPL帮助类
    /// </summary>
    public class ZebraHelper
    {
        /*
         *  打印中文先引用Fnthex32.dll
            dll控件常规安装方法(仅供参考):
            下面是32系统的注册bat文件
            可将下面的代码保存为“注册.bat“,放到dll目录,就会自动完成dll注册(win98不支持)。
            @echo 开始注册
            copy Fnthex32.dll %windir%\system32\
            regsvr32 %windir%\system32\Fnthex32.dll /s
            @echo Fnthex32.dll注册成功
            @pause

            下面是64系统的注册bat文件
            @echo 开始注册
            copy Fnthex32.dll %windir%\SysWOW64\
            regsvr32 %windir%\SysWOW64\Fnthex32.dll /s
            @echo Fnthex32.dll注册成功
            @pause
         * 
         * 
            ZebraHelper zh = new ZebraHelper();
            StringBuilder builder = new StringBuilder();            
            builder.AppendLine(zh.ZPL_Start());
            builder.AppendLine(zh.ZPL_PageSet(40, 80));
            builder.AppendLine(zh.ZPL_DrawCHText("上善若水 厚德载物", "宋体", 40, 40, 0, 32, 0, 1, 0));
            builder.AppendLine(zh.ZPL_DrawBarcode(40, 150, 3, 2, 40, "111112222233333"));
            builder.AppendLine(zh.ZPL_DrawENText("111112222233333", "A", 30, 205, "N", 30, 50));
            builder.AppendLine(zh.ZPL_DrawRectangle(20,20,2,700,700));
            builder.AppendLine(zh.ZPL_End());
            string a = builder.ToString();
            //打印
            zh.CmdDos("c:\\c.txt", a);         
         */
        public string ZPL_Start()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("^XA"); //指令块的开始
            //builder.AppendLine("^MD30"); //MD是设置色带颜色的深度
            return builder.ToString();
        }

        public string ZPL_End()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("^XZ"); //指令块的结束
            return builder.ToString();
        }

        /// <summary>
        ///  设置打印标签纸边距
        /// </summary>
        /// <param name="printX">标签纸边距x坐标</param>
        /// <param name="printY">标签纸边距y坐标</param>
        /// <returns></returns>
        public string ZPL_PageSet(int printX, int printY)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("^LH" + printX + "," + printY); //定义条码纸边距  80 10
            return builder.ToString();
        }

        /// <summary>
        /// 打印凭条设置
        /// </summary>
        /// <param name="width">凭条宽度</param>
        /// <param name="height">凭条高度</param>
        /// <returns>返回ZPL命令</returns>
        public string ZPL_SetLabel(int width, int height)
        {
            //ZPL条码设置命令:^PW640^LL480
            string sReturn = "^PW{0}^LL{1}";
            return string.Format(sReturn, width, height);
        }

        /// <summary>
        ///  打印矩形
        /// </summary>
        /// <param name="px">起点X坐标</param>
        /// <param name="py">起点Y坐标</param>
        /// <param name="thickness">边框宽度</param>
        /// <param name="width">矩形宽度,0表示打印一条竖线</param>
        /// <param name="height">矩形高度,0表示打印一条横线</param>
        /// <returns>返回ZPL命令</returns>
        public string ZPL_DrawRectangle(int px, int py, int thickness, int width, int height)
        {
            //ZPL矩形命令:^FO50,50^GB300,200,2^FS
            string sReturn = "^FO{0},{1}^GB{3},{4},{2}^FS";
            return string.Format(sReturn, px, py, thickness, width, height);
        }

        /// <summary>
        /// 打印英文
        /// </summary>
        /// <param name="EnText">待打印文本</param>
        /// <param name="ZebraFont">打印机字体 A-Z</param>
        /// <param name="px">起点X坐标</param>
        /// <param name="py">起点Y坐标</param>
        /// <param name="Orient">旋转角度N = normal,R = rotated 90 degrees (clockwise),I = inverted 180 degrees,B = read from bottom up, 270 degrees</param>
        /// <param name="Height">字体高度</param>
        /// <param name="Width">字体宽度</param>
        /// <returns>返回ZPL命令</returns>
        public string ZPL_DrawENText(string EnText, string ZebraFont, int px, int py, string Orient, int Height, int Width)
        {
            //ZPL打印英文命令:^FO50,50^A0N,32,25^FDZEBRA^FS
            string sReturn = "^FO{1},{2}^A" + ZebraFont + "{3},{4},{5}^FD{0}^FS";
            return string.Format(sReturn, EnText, px, py, Orient, Height, Width);
        }


        /// <summary>
        /// 中文处理,返回ZPL命令
        /// </summary>
        /// <param name="ChineseText">待转变中文内容</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="startX">X坐标</param>
        /// <param name="startY">Y坐标</param>
        /// <param name="Orient">旋转角度0,90,180,270</param>
        /// <param name="Height">字体高度</param>
        /// <param name="Width">字体宽度,通常是0</param>
        /// <param name="IsBold">1 变粗,0 正常</param>
        /// <param name="IsItalic">1 斜体,0 正常</param>
        /// <returns></returns>
        public string ZPL_DrawCHText(string ChineseText, string FontName, int startX, int startY, int Orient, int Height, int Width, int IsBold, int IsItalic)
        {
            StringBuilder sResult = new StringBuilder();
            StringBuilder hexbuf = new StringBuilder(21 * 1024);
            int count = ZebraHelper.GETFONTHEX(ChineseText, FontName, Orient, Height, Width, IsBold, IsItalic, hexbuf);
            if (count > 0)
            {
                string sEnd = "^FO" + startX.ToString() + "," + startY.ToString() + "^XGOUTSTR" + ",1,2^FS ";
                sResult.AppendLine(hexbuf.ToString().Replace("OUTSTR01", "OUTSTR") + sEnd);
            }
            return sResult.ToString();
        }

        /// <summary>
        /// 打印条形码(128码)
        /// </summary>
        /// <param name="px">起点X坐标</param>
        /// <param name="py">起点Y坐标</param>
        /// <param name="width">基本单元宽度 1-10</param>
        /// <param name="ratio">宽窄比 2.0-3.0 增量0.1</param>
        /// <param name="barheight">条码高度</param>
        /// <param name="barcode">条码内容</param>
        /// <returns>返回ZPL命令</returns>
        public string ZPL_DrawBarcode(int px, int py, int width, int ratio, int barheight, string barcode)
        {
            //ZPL打印英文命令:^FO50,260^BY1,2^BCN,100,Y,N^FDSMJH2000544610^FS
            string sReturn = "^FO{0},{1}^BY{2},{3}^BCN,{4},N,N,N,A^FD{5}^FS";
            return string.Format(sReturn, px, py, width, ratio, barheight, barcode);
        }
        /// <summary>
        /// 二维码列印
        /// </summary>
        /// <param name="px">做边距</param>
        /// <param name="py">上边距</param>
        /// <param name="barcode">条码内容</param>
        /// <returns></returns>
        public string ZPL_Draw2DBarcode(int px, int py, string barcode)
        {
            //5^FDQA  二维码大小
            string sReturn = "^FO{0},{1}^BQ,2,3^FDHA,{2}^FS";
            return string.Format(sReturn, px, py, barcode);
        }


        /// <summary>
        /// 打印图片
        /// </summary>
        /// <param name="px"></param>
        /// <param name="py"></param>
        /// <param name="s_FilePath"></param>
        /// <returns></returns>
        public string ZPL_DrawImage(int px, int py, string s_FilePath)
        {
            int b = 0;
            long n = 0;
            long clr;
            StringBuilder sb = new StringBuilder();
            sb.Append("~DGR:ZONE.GRF,");
            Bitmap bm = new Bitmap(s_FilePath);
            int w = ((bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)) * bm.Size.Height);
            int h = (bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1));

            sb.Append(w.ToString().PadLeft(5, '0') + "," + h.ToString().PadLeft(3, '0') + ", ");
            using (Bitmap bmp = new Bitmap(bm.Size.Width, bm.Size.Height))
            {
                for (int y = 0; y < bm.Size.Height; y++)
                {
                    for (int x = 0; x < bm.Size.Width; x++)
                    {
                        b = b * 2;
                        clr = bm.GetPixel(x, y).ToArgb();
                        string s = clr.ToString("X");

                        if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
                        {
                            bmp.SetPixel(x, y, bm.GetPixel(x, y));
                            b++;
                        }
                        n++;
                        if (x == (bm.Size.Width - 1))
                        {
                            if (n < 8)
                            {
                                b = b * (2 ^ (8 - (int)n));

                                sb.Append(b.ToString("X").PadLeft(2, '0'));
                                b = 0;
                                n = 0;
                            }
                        }
                        if (n >= 8)
                        {
                            sb.Append(b.ToString("X").PadLeft(2, '0'));
                            b = 0;
                            n = 0;
                        }
                    }
                }
                sb.Append(string.Format("^LH0,0^FO{0},{1}^XGR:ZONE.GRF^FS", px, py));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 把文字转换才Bitmap
        /// </summary>
        /// <param name="text"></param>
        /// <param name="font"></param>
        /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
        /// <param name="fontcolor">字体颜色</param>
        /// <param name="backColor">背景颜色</param>
        /// <returns></returns>
        private Bitmap TextToBitmap(string text, Font font, Rectangle rect, Color fontcolor, Color backColor)
        {
            Graphics g;
            Bitmap bmp;
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            if (rect == Rectangle.Empty)
            {
                bmp = new Bitmap(1, 1);
                g = Graphics.FromImage(bmp);
                //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
                SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);

                int width = (int)(sizef.Width + 1);
                int height = (int)(sizef.Height + 1);
                rect = new Rectangle(0, 0, width, height);
                bmp.Dispose();

                bmp = new Bitmap(width, height);
            }
            else
            {
                bmp = new Bitmap(rect.Width, rect.Height);
            }

            g = Graphics.FromImage(bmp);

            //使用ClearType字体功能
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.FillRectangle(new SolidBrush(backColor), rect);
            g.DrawString(text, font, Brushes.Black, rect, format);
            return bmp;
        }

        /*
             中文或其它复杂设计成图片,然后用ZPL命令发送给条码打印机打印             
            //定义字体  
            Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);
            //生成图片
            Bitmap img = CreateImage("出厂日期:" + DateTime.Now, drawFont);
            var imgCode = ConvertImageToCode(img);
            var t = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); //图形中的总字节数
            var w = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); //每行的字节数
            string zpl = string.Format("~DGR:imgName.GRF,{0},{1},{2}", t, w, imgCode); //发送给打印机
        */
        /// <summary>
        /// 序列化图片
        /// </summary>
        /// <param name="img">Bitmap</param>
        /// <returns></returns>
        public string ConvertImageToCode(Bitmap img)
        {
            var sb = new StringBuilder();
            long clr = 0, n = 0;
            int b = 0;
            for (int i = 0; i < img.Size.Height; i++)
            {
                for (int j = 0; j < img.Size.Width; j++)
                {
                    b = b * 2;
                    clr = img.GetPixel(j, i).ToArgb();
                    string s = clr.ToString("X");

                    if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
                    {
                        b++;
                    }
                    n++;
                    if (j == (img.Size.Width - 1))
                    {
                        if (n < 8)
                        {
                            b = b * (2 ^ (8 - (int)n));

                            sb.Append(b.ToString("X").PadLeft(2, '0'));
                            b = 0;
                            n = 0;
                        }
                    }
                    if (n >= 8)
                    {
                        sb.Append(b.ToString("X").PadLeft(2, '0'));
                        b = 0;
                        n = 0;
                    }
                }
                sb.Append(System.Environment.NewLine);
            }
            return sb.ToString();
        } 

        /// <summary>
        /// 中文处理
        /// </summary>
        /// <param name="ChineseText">待转变中文内容</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="Orient">旋转角度0,90,180,270</param>
        /// <param name="Height">字体高度</param>
        /// <param name="Width">字体宽度,通常是0</param>
        /// <param name="IsBold">1 变粗,0 正常</param>
        /// <param name="IsItalic">1 斜体,0 正常</param>
        /// <param name="ReturnPicData">返回的图片字符</param>
        /// <returns></returns>
        [DllImport("fnthex32.dll")]
        public static extern int GETFONTHEX(
            string ChineseText,
            string FontName,
            int Orient,
            int Height,
            int Width,
            int IsBold,
            int IsItalic,
            StringBuilder ReturnPicData);

        /// <summary>
        /// 中文处理
        /// </summary>
        /// <param name="ChineseText">待转变中文内容</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="FileName">返回的图片字符重命</param>
        /// <param name="Orient">旋转角度0,90,180,270</param>
        /// <param name="Height">字体高度</param>
        /// <param name="Width">字体宽度,通常是0</param>
        /// <param name="IsBold">1 变粗,0 正常</param>
        /// <param name="IsItalic">1 斜体,0 正常</param>
        /// <param name="ReturnPicData">返回的图片字符</param>
        /// <returns></returns>
        [DllImport("fnthex32.dll")]
        public static extern int GETFONTHEX(
                              string ChineseText,
                              string FontName,
                              string FileName,
                              int Orient,
                              int Height,
                              int Width,
                              int IsBold,
                              int IsItalic,
                              StringBuilder ReturnPicData);

        #region 扩展
        /// <summary>
        /// 毫米转像素 取整
        /// </summary>
        /// <param name="mm">毫米</param>
        /// <param name="dpi">打印DPI 如300</param>
        /// <returns></returns>
        public double mm2px(double mm, double dpi)
        {
            double px = (mm / 25.4) * dpi;
            return Math.Round(px, 0, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 像素转毫米 取整
        /// </summary>
        /// <param name="px">像素</param>
        /// <param name="dpi">打印DPI 如300</param>
        /// <returns></returns>
        public double px2mm(double px, double dpi)
        {
            //像素转换成毫米公式:(宽度像素/水平DPI)*25.4;
            double mm = (px / dpi) * 25.4;
            return Math.Round(mm, 0, MidpointRounding.AwayFromZero);
        }
        #endregion
    }
}

2、ZebraPrintHelper(推荐)适用于标签、票据、条码打印

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;
using System.Net.Sockets;
namespace Common
{
    #region 定义设备类型枚举
    public enum DeviceType
    {
        COM = 0,
        LPT = 1,
        DRV = 2,
        TCP = 3
    }
    #endregion

    #region 定义打印机指令类型枚举
    public enum ProgrammingLanguage
    {
        ZPL = 0,
        EPL = 1
    }
    #endregion

    #region 定义日志类型枚举
    public enum LogType
    {
        Print = 0,
        Error = 1
    }
    #endregion

    #region 定义打印文档信息类
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class DocInfo
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string DocName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string OutputFile;
        [MarshalAs(UnmanagedType.LPStr)]
        public string DataType;
    }
    #endregion

    #region 定义图像设备信息类
    public class DeviceInfo
    {
        #region 属性说明
        /* 
        ColorDepth  
            图像输出支持的颜色范围的像素深度。有效值为 1、4、8、24 和 32。默认值为 24。仅对 TIFF 呈现支持 ColorDepth,对于其他图像输出格式报表服务器将忽略此设置。 
 
        注意:  
        对于此版本的 SQL Server,此设置的值将被忽略,且通常将 TIFF 图像呈现为 24 位。 
  
        Columns  
            要为报表设置的列数。此值将覆盖报表的原始设置。 
  
        ColumnSpacing  
            要为报表设置的列间距。此值将覆盖报表的原始设置。 
  
        DpiX  
            输出设备在 X 方向的分辨率。默认值为 96。 
  
        DpiY  
            输出设备在 Y 方向的分辨率。默认值为 96。 
  
        EndPage  
            要呈现的报表的最后一页。默认值为 StartPage 的值。 
  
        MarginBottom  
            要为报表设置的下边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  
        MarginLeft  
            要为报表设置的左边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  
        MarginRight  
            要为报表设置的右边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  
        MarginTop  
            要为报表设置的上边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  
        OutputFormat  
            图形设备接口 (GDI) 支持的输出格式之一:BMP、EMF、GIF、JPEG、PNG 或 TIFF。 
  
        PageHeight  
            要为报表设置的页高,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,11in)。此值将覆盖报表的原始设置。 
  
        PageWidth  
            要为报表设置的页宽,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,8.5in)。此值将覆盖报表的原始设置。 
  
        StartPage  
            要呈现的报告的第一页。值为 0 指示将呈现所有页。默认值为 1。  
         */
        #endregion

        public enum GDIOutputFormat { BMP, EMF, GIF, JPEG, PNG, TIFF }

        public int ColorDepth { get; set; }
        public int Columns { get; set; }
        public int ColumnSpacing { get; set; }
        public int DpiX { get; set; }
        public int DpiY { get; set; }
        public int EndPage { get; set; }
        public int MarginBottom { get; set; }
        public int MarginLeft { get; set; }
        public int MarginRight { get; set; }
        public int MarginTop { get; set; }
        public GDIOutputFormat OutputFormat { get; set; }
        public int PageHeight { get; set; }
        public int PageWidth { get; set; }
        public int StartPage { get; set; }

        private const string xmlFormater = @"<DeviceInfo>  
                <ColorDepth>{0}</ColorDepth>  
                <Columns>{1}</Columns>  
                <ColumnSpacing>{2}</ColumnSpacing>  
                <DpiX>{3}</DpiX>  
                <DpiY>{4}</DpiY>  
                <EndPage>{5}</EndPage>  
                <MarginBottom>{6}</MarginBottom>  
                <MarginLeft>{7}</MarginLeft>  
                <MarginRight>{8}</MarginRight>  
                <MarginTop>{9}</MarginTop>  
                <OutputFormat>{10}</OutputFormat>  
                <PageHeight>{11}</PageHeight>  
                <PageWidth>{12}</PageWidth>  
                <StartPage>{13}</StartPage>  
                </DeviceInfo>";

        public DeviceInfo()
        {
            this.ColorDepth = 24;
            this.Columns = 0;
            this.StartPage = 1;
            this.EndPage = 1;
        }

        public string GetDeviceInfo()
        {
            string result = string.Format(xmlFormater,
                this.ColorDepth,
                this.Columns,
                this.ColumnSpacing,
                this.DpiX,
                this.DpiY,
                this.EndPage,
                this.MarginBottom,
                this.MarginLeft,
                this.MarginRight,
                this.MarginTop,
                this.OutputFormat,
                this.PageHeight,
                this.PageWidth,
                this.StartPage);
            return result;
        }

        public string GetDeviceInfoForImage()
        {
            string result = string.Format("<DeviceInfo><StartPage>{0}</StartPage><EndPage>{1}</EndPage><OutputFormat>{2}</OutputFormat><DpiX>{3}</DpiX><DpiY>{4}</DpiY></DeviceInfo>",
                this.StartPage,
                this.EndPage,
                this.OutputFormat,
                this.DpiX,
                this.DpiY);
            return result;
        }
    }
    #endregion

    #region 定义斑马打印助手类
    /// <summary>  
    /// 斑马打印助手,支持LPT/COM/DRV三种模式,适用于标签、票据、条码打印。  
    /// </summary>  
    public static class ZebraPrintHelper
    {
        #region 定义API方法

        #region 写打印口(LPT)方法
        private const short FILE_ATTRIBUTE_NORMAL = 0x80;
        private const short INVALID_HANDLE_VALUE = -1;
        private const uint GENERIC_READ = 0x80000000;
        private const uint GENERIC_WRITE = 0x40000000;
        private const uint CREATE_NEW = 1;
        private const uint CREATE_ALWAYS = 2;
        private const uint OPEN_EXISTING = 3;
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern SafeFileHandle CreateFile(string strFileName,
            uint dwDesiredAccess,
            uint dwShareMode,
            IntPtr intptrSecurityAttributes,
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes,
            IntPtr intptrTemplateFile);
        #endregion

        [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string printerName, out IntPtr intptrPrinter, IntPtr intptrPrintDocument);

        [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool ClosePrinter(IntPtr intptrPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartDocPrinter(IntPtr intptrPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DocInfo docInfo);

        [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndDocPrinter(IntPtr intptrPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartPagePrinter(IntPtr intptrPrinter);

        [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndPagePrinter(IntPtr intptrPrinter);

        [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool WritePrinter(IntPtr intptrPrinter, IntPtr intptrBytes, Int32 count, out Int32 written);
        #endregion

        #region 定义私有字段

        /// <summary>  
        /// 线程锁,防止多线程调用。  
        /// </summary>  
        private static object SyncRoot = new object();

        /// <summary>  
        /// 字节流传递时采用的字符编码  
        /// </summary>  
        private static readonly Encoding TransferFormat = Encoding.GetEncoding("iso-8859-1");

        #endregion

        #region 定义属性
        public static string TcpIpAddress { get; set; }
        public static int TcpPort { get; set; }
        public static int Port { get; set; }
        public static string PrinterName { get; set; }
        public static bool IsWriteLog { get; set; }
        public static DeviceType PrinterType { get; set; }
        public static ProgrammingLanguage PrinterProgrammingLanguage { get; set; }

        /// <summary>  
        /// 日志保存目录,WEB应用注意不能放在BIN目录下。  
        /// </summary>  
        public static string LogsDirectory { get; set; }

        private static byte[] GraphBuffer { get; set; }
        private static int GraphWidth { get; set; }
        private static int GraphHeight { get; set; }

        private static int RowSize
        {
            get
            {
                return (((GraphWidth) + 31) >> 5) << 2;
            }
        }

        private static int RowRealBytesCount
        {
            get
            {
                if ((GraphWidth % 8) > 0)
                {
                    return GraphWidth / 8 + 1;
                }
                else
                {
                    return GraphWidth / 8;
                }
            }
        }
        #endregion

        #region 静态构造方法
        static ZebraPrintHelper()
        {
            GraphBuffer = new byte[0];
            IsWriteLog = false;
            LogsDirectory = "logs";
        }
        #endregion

        #region 定义发送原始数据到打印机的方法
        private static bool SendBytesToPrinter(string printerName, IntPtr intptrBytes, Int32 count)
        {
            Int32 error = 0, written = 0;
            IntPtr intptrPrinter = new IntPtr(0);
            DocInfo docInfo = new DocInfo();
            bool bSuccess = false;

            docInfo.DocName = ".NET RAW Document";
            docInfo.DataType = "RAW";

            // Open the printer.  
            if (OpenPrinter(printerName.Normalize(), out intptrPrinter, IntPtr.Zero))
            {
                // Start a document.  
                if (StartDocPrinter(intptrPrinter, 1, docInfo))
                {
                    // Start a page.  
                    if (StartPagePrinter(intptrPrinter))
                    {
                        // Write your bytes.  
                        bSuccess = WritePrinter(intptrPrinter, intptrBytes, count, out written);
                        EndPagePrinter(intptrPrinter);
                    }
                    EndDocPrinter(intptrPrinter);
                }
                ClosePrinter(intptrPrinter);
            }
            // If you did not succeed, GetLastError may give more information  
            // about why not.  
            if (bSuccess == false)
            {
                error = Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }

        public static bool SendFileToPrinter(string printerName, string fileName)
        {
            // Open the file.  
            FileStream fs = new FileStream(fileName, FileMode.Open);
            // Create a BinaryReader on the file.  
            BinaryReader br = new BinaryReader(fs);
            // Dim an array of bytes big enough to hold the file's contents.  
            Byte[] bytes = new Byte[fs.Length];
            bool bSuccess = false;
            // Your unmanaged pointer.  
            IntPtr pUnmanagedBytes = new IntPtr(0);
            int nLength;

            nLength = Convert.ToInt32(fs.Length);
            // Read the contents of the file into the array.  
            bytes = br.ReadBytes(nLength);
            // Allocate some unmanaged memory for those bytes.  
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.  
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.  
            bSuccess = SendBytesToPrinter(printerName, pUnmanagedBytes, nLength);
            // Free the unmanaged memory that you allocated earlier.  
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return bSuccess;
        }

        public static bool SendBytesToPrinter(string printerName, byte[] bytes)
        {
            bool bSuccess = false;
            IntPtr pUnmanagedBytes = new IntPtr(0);
            int nLength = bytes.Length;
            // Allocate some unmanaged memory for those bytes.  
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.  
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.  
            bSuccess = SendBytesToPrinter(printerName, pUnmanagedBytes, nLength);
            // Free the unmanaged memory that you allocated earlier.  
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return bSuccess;
        }

        public static bool SendStringToPrinter(string printerName, string text)
        {
            IntPtr pBytes;
            Int32 dwCount;
            // How many characters are in the string?  
            dwCount = (text.Length + 1) * Marshal.SystemMaxDBCSCharSize;
            // Assume that the printer is expecting ANSI text, and then convert  
            // the string to ANSI text.  
            pBytes = Marshal.StringToCoTaskMemAnsi(text);
            // Send the converted ANSI string to the printer.  
            SendBytesToPrinter(printerName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
        #endregion

        #region 日志记录方法
        private static void WriteLog(string text, LogType logType)
        {
            string endTag = string.Format("\r\n{0}\r\n", new string('=', 80));
            string path = string.Format("{0}\\{1}-{2}.log", LogsDirectory, DateTime.Now.ToString("yyyy-MM-dd"), logType);
            if (!Directory.Exists(LogsDirectory))
            {
                Directory.CreateDirectory(LogsDirectory);
            }
            if (logType == LogType.Error)
            {
                File.AppendAllText(path, string.Format("{0}{1}", text, endTag), Encoding.Default);
            }
            if (logType == LogType.Print)
            {
                if (text.StartsWith("N\r\nGW"))
                {
                    using (FileStream fs = new FileStream(path, FileMode.Append))
                    {
                        byte[] bytes = TransferFormat.GetBytes(text);
                        byte[] tag = TransferFormat.GetBytes(endTag);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Write(tag, 0, tag.Length);
                        fs.Close();
                    }
                }
                else
                {
                    File.AppendAllText(path, string.Format("{0}{1}", text, endTag), Encoding.Default);
                }
            }
        }

        private static void WriteLog(byte[] bytes, LogType logType)
        {
            string endTag = string.Format("\r\n{0}\r\n", new string('=', 80));
            string path = string.Format("{0}\\{1}-{2}.log", LogsDirectory, DateTime.Now.ToString("yyyy-MM-dd"), logType);
            if (!Directory.Exists(LogsDirectory))
            {
                Directory.CreateDirectory(LogsDirectory);
            }
            if (logType == LogType.Error)
            {
                File.AppendAllText(path, string.Format("{0}{1}", Encoding.Default.GetString(bytes), endTag), Encoding.Default);
            }
            if (logType == LogType.Print)
            {
                string transferFormat = TransferFormat.GetString(bytes);
                if (transferFormat.StartsWith("N\r\nGW"))
                {
                    using (FileStream fs = new FileStream(path, FileMode.Append))
                    {
                        byte[] tag = TransferFormat.GetBytes(endTag);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Write(tag, 0, tag.Length);
                        fs.Close();
                    }
                }
                else
                {
                    File.AppendAllText(path, string.Format("{0}{1}", Encoding.Default.GetString(bytes), endTag), Encoding.Default);
                }
            }
        }
        #endregion

        #region 封装方法,方便调用。
        public static bool PrintWithCOM(string cmd, int port, bool isWriteLog)
        {
            PrinterType = DeviceType.COM;
            Port = port;
            IsWriteLog = isWriteLog;
            return PrintCommand(cmd);
        }

        public static bool PrintWithCOM(byte[] bytes, int port, bool isWriteLog, ProgrammingLanguage progLanguage)
        {
            PrinterType = DeviceType.COM;
            Port = port;
            IsWriteLog = isWriteLog;
            PrinterProgrammingLanguage = progLanguage;
            return PrintGraphics(bytes);
        }

        public static bool PrintWithLPT(string cmd, int port, bool isWriteLog)
        {
            PrinterType = DeviceType.LPT;
            Port = port;
            IsWriteLog = isWriteLog;
            return PrintCommand(cmd);
        }

        public static bool PrintWithLPT(byte[] bytes, int port, bool isWriteLog, ProgrammingLanguage progLanguage)
        {
            PrinterType = DeviceType.LPT;
            Port = port;
            IsWriteLog = isWriteLog;
            PrinterProgrammingLanguage = progLanguage;
            return PrintGraphics(bytes);
        }

        public static bool PrintWithTCP(string cmd, bool isWriteLog)
        {
            PrinterType = DeviceType.TCP;
            IsWriteLog = isWriteLog;
            return PrintCommand(cmd);
        }

        public static bool PrintWithTCP(byte[] bytes, bool isWriteLog)
        {
            PrinterType = DeviceType.TCP;
            IsWriteLog = isWriteLog;
            return PrintGraphics(bytes);
        }  
        public static bool PrintWithDRV(string cmd, string printerName, bool isWriteLog)
        {
            PrinterType = DeviceType.DRV;
            PrinterName = printerName;
            IsWriteLog = isWriteLog;
            return PrintCommand(cmd);
        }

        public static bool PrintWithDRV(byte[] bytes, string printerName, bool isWriteLog, ProgrammingLanguage progLanguage)
        {
            PrinterType = DeviceType.DRV;
            PrinterName = printerName;
            IsWriteLog = isWriteLog;
            PrinterProgrammingLanguage = progLanguage;
            return PrintGraphics(bytes);
        }
        #endregion

        #region 打印ZPL、EPL、CPCL、TCP指令
        public static bool PrintCommand(string cmd)
        {
            lock (SyncRoot)
            {
                bool result = false;
                try
                {
                    switch (PrinterType)
                    {
                        case DeviceType.COM:
                            result = comPrint(Encoding.Default.GetBytes(cmd));
                            break;
                        case DeviceType.LPT:
                            result = lptPrint(Encoding.Default.GetBytes(cmd));
                            break;
                        case DeviceType.DRV:
                            result = drvPrint(Encoding.Default.GetBytes(cmd));
                            break;
                        case DeviceType.TCP:
                            result = tcpPrint(Encoding.Default.GetBytes(cmd));
                            break;
                    }
                    if (!string.IsNullOrEmpty(cmd) && IsWriteLog)
                    {
                        WriteLog(cmd, LogType.Print);
                    }
                }
                catch (Exception ex)
                {
                    //记录日志  
                    if (IsWriteLog)
                    {
                        WriteLog(string.Format("{0} => {1}\r\n{2}", DateTime.Now, ex.Message, ex), LogType.Error);
                    }
                    throw new Exception(ex.Message, ex);
                }
                finally
                {
                    GraphBuffer = new byte[0];
                }
                return result;
            }
        }
        #endregion  

        #region 打印图像字节流
        public static bool PrintGraphics(byte[] graph)
        {
            lock (SyncRoot)
            {
                bool result = false;
                try
                {
                    GraphBuffer = graph;
                    byte[] cmdBytes = new byte[0];
                    if (PrinterProgrammingLanguage == ProgrammingLanguage.ZPL)
                    {
                        cmdBytes = getZPLBytes();
                    }
                    if (PrinterProgrammingLanguage == ProgrammingLanguage.EPL)
                    {
                        cmdBytes = getEPLBytes();
                    }
                    switch (PrinterType)
                    {
                        case DeviceType.COM:
                            result = comPrint(cmdBytes);
                            break;
                        case DeviceType.LPT:
                            result = lptPrint(cmdBytes);
                            break;
                        case DeviceType.DRV:
                            result = drvPrint(cmdBytes);
                            break;
                        case DeviceType.TCP:
                            result = tcpPrint(cmdBytes);
                            break;
                    }
                    if (cmdBytes.Length > 0 && IsWriteLog)
                    {
                        WriteLog(cmdBytes, LogType.Print);
                    }
                }
                catch (Exception ex)
                {
                    //记录日志  
                    if (IsWriteLog)
                    {
                        WriteLog(string.Format("{0} => {1}\r\n{2}", DateTime.Now, ex.Message, ex), LogType.Error);
                    }
                }
                finally
                {
                    GraphBuffer = new byte[0];
                }
                return result;
            }
        }
        #endregion

        #region COM/LPT/DRV/TCP四种模式打印方法
        private static bool drvPrint(byte[] cmdBytes)
        {
            bool result = false;
            try
            {
                if (!string.IsNullOrEmpty(PrinterName))
                {
                    result = SendBytesToPrinter(PrinterName, cmdBytes);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private static bool comPrint(byte[] cmdBytes)
        {
            bool result = false;
            SerialPort com = new SerialPort(string.Format("{0}{1}", PrinterType, Port), 9600, Parity.None, 8, StopBits.One);
            try
            {
                com.Open();
                com.Write(cmdBytes, 0, cmdBytes.Length);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (com.IsOpen)
                {
                    com.Close();
                }
            }
            return result;
        }

        private static bool lptPrint(byte[] cmdBytes)
        {
            bool result = false;
            FileStream fileStream = null;
            StreamWriter streamWriter = null;
            SafeFileHandle handle = null;
            try
            {
                handle = CreateFile(string.Format("{0}{1}", PrinterType, Port), GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
                if (!handle.IsInvalid)
                {
                    fileStream = new FileStream(handle, FileAccess.ReadWrite);
                    streamWriter = new StreamWriter(fileStream, Encoding.Default);
                    streamWriter.Write(cmdBytes);
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream = null;
                }
                if (streamWriter != null)
                {
                    streamWriter.Close();
                    streamWriter = null;
                }
                if (handle != null)
                {
                    handle.Close();
                    handle = null;
                }
            }
            return result;
        }

        private static bool tcpPrint(byte[] cmdBytes)
        {
            bool result = false;
            TcpClient tcp = null;
            try
            {
                tcp = TimeOutSocket.Connect(TcpIpAddress, TcpPort, 1000);
                tcp.SendTimeout = 1000;
                tcp.ReceiveTimeout = 1000;
                if (tcp.Connected)
                {
                    tcp.Client.Send(cmdBytes);
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("打印失败,请检查打印机或网络设置。", ex);
            }
            finally
            {
                if (tcp != null)
                {
                    if (tcp.Client != null)
                    {
                        tcp.Client.Close();
                        tcp.Client = null;
                    }
                    tcp.Close();
                    tcp = null;
                }
            }
            return result;
        }  
        #endregion

        #region 生成ZPL图像打印指令
        private static byte[] getZPLBytes()
        {
            byte[] result = new byte[0];
            byte[] bmpData = getBitmapData();
            string textBitmap = string.Empty;
            string textHex = BitConverter.ToString(bmpData).Replace("-", string.Empty);
            for (int i = 0; i < GraphHeight; i++)
            {
                textBitmap += textHex.Substring(i * RowRealBytesCount * 2, RowRealBytesCount * 2) + "\r\n";
            }
            string text = string.Format("~DGR:IMAGE.GRF,{0},{1},\r\n{2}^XGR:IMAGE.GRF,1,1^FS\r\n^IDR:IMAGE.GRF\r\n",
                GraphHeight * RowRealBytesCount,
                RowRealBytesCount,
                textBitmap);
            result = Encoding.Default.GetBytes(text);
            return result;
        }
        #endregion

        #region 生成EPL图像打印指令
        private static byte[] getEPLBytes()
        {
            byte[] result = new byte[0];
            byte[] buffer = getBitmapData();
            string text = string.Format("N\r\nGW{0},{1},{2},{3},{4}\r\nP\r\n",
                0,
                0,
                RowRealBytesCount,
                GraphHeight,
                TransferFormat.GetString(buffer));
            result = TransferFormat.GetBytes(text);
            return result;
        }
        #endregion

        #region 获取单色位图数据
        /// <summary>  
        /// 获取单色位图数据(1bpp),不含文件头、信息头、调色板三类数据。  
        /// </summary>  
        /// <returns></returns>  
        private static byte[] getBitmapData()
        {
            MemoryStream srcStream = new MemoryStream();
            MemoryStream dstStream = new MemoryStream();
            Bitmap srcBmp = null;
            Bitmap dstBmp = null;
            byte[] srcBuffer = null;
            byte[] dstBuffer = null;
            byte[] result = null;
            try
            {
                srcStream = new MemoryStream(GraphBuffer);
                srcBmp = Bitmap.FromStream(srcStream) as Bitmap;
                srcBuffer = srcStream.ToArray();
                GraphWidth = srcBmp.Width;
                GraphHeight = srcBmp.Height;
                dstBmp = srcBmp.Clone(new Rectangle(0, 0, srcBmp.Width, srcBmp.Height), PixelFormat.Format1bppIndexed);
                dstBmp.Save(dstStream, ImageFormat.Bmp);
                dstBuffer = dstStream.ToArray();

                int bfSize = BitConverter.ToInt32(dstBuffer, 2);
                int bfOffBits = BitConverter.ToInt32(dstBuffer, 10);
                int bitmapDataLength = bfSize - bfOffBits;
                result = new byte[GraphHeight * RowRealBytesCount];

                //读取时需要反向读取每行字节实现上下翻转的效果,打印机打印顺序需要这样读取。  
                for (int i = 0; i < GraphHeight; i++)
                {
                    Array.Copy(dstBuffer, bfOffBits + (GraphHeight - 1 - i) * RowSize, result, i * RowRealBytesCount, RowRealBytesCount);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (srcStream != null)
                {
                    srcStream.Dispose();
                    srcStream = null;
                }
                if (dstStream != null)
                {
                    dstStream.Dispose();
                    dstStream = null;
                }
                if (srcBmp != null)
                {
                    srcBmp.Dispose();
                    srcBmp = null;
                }
                if (dstBmp != null)
                {
                    dstBmp.Dispose();
                    dstBmp = null;
                }
            }
            return result;
        }
        #endregion
    }
    #endregion
}

标签:string,Helper,C#,LPT,int,static,new,bool,public
From: https://www.cnblogs.com/qq2806933146xiaobai/p/17321619.html

相关文章

  • Vue3中(vite.config.js)配置打包的时候去除console.log
    参考:https://www.cnblogs.com/lovewhatIlove/p/16476165.html安装tersernpmadd-Dterservite中配置import{defineConfig}from"vite";importvuefrom"@vitejs/plugin-vue";importvueJsxfrom"@vitejs/plugin-vue-jsx";importvisua......
  • docker2
    1容器操作#启动容器 dockerstart容器id#停止容器 dockerstop容器id#文件拷贝 #容器的文件copy到宿主机上(不是在容器内执行)dockercp容器名称:容器目录需要拷贝的文件或目录dockercp20fd8:/home/lqz.txt/home/lqz.txt#宿主机的文件copy......
  • coc-settings中设置自定义头文件路径
    具体方案例如{"suggest.noselect":true,"languageserver":{"ccls":{"command":"ccls","filetypes":["cc","c","cpp","cuda"]......
  • 网页滚动体验,IScroll滚动插件,你安装了类似的滚动页面插件吗
    IScroll是一款基于JavaScript的插件,用于在网页中实现平滑滚动效果。这个插件可以帮助用户创建回到页面顶部和底部的按钮、生成页面导航快照,以及设置滚动时间等功能,从而提升网页的用户体验。IScroll的特点在于,它能够平滑地滚动网页内容,而不会像传统的滚动条那样跳跃。此外,该插件可以......
  • Java笔记(16) Collection集合-->Set集合-->HashSet
    1.Set接口基本介绍Set是无序集合(添加和取出的顺序不一致,但取出的顺序是固定的),没有索引不允许重复元素,所以最多包含一个nullJDKAPI中Set接口的实现类有:Abstract,ConcurrentHashMap.KeySetView,ConcurrentSkipListSet,CopyOnWriteArraySet,EnumSet,HashSet,JobStateRea......
  • Buuctf刷题Web 基础1
    1、[极客大挑战2019]EasySQL1题目,页面很酷炫!尝试一下万能密码提交得到flag{a21e57ef-6b2f-443e-a2fa-74635e6ce5aa}2、[极客大挑战2019]Havefun1题目是很可爱的一只小猫没有什么发现,F12检查一下于是乎传参http://84ceb43a-fd23-433b-9e30-84c174fd9b94.node4.buuoj.cn:81/?cat=......
  • Spring Boot Configuration Annotation Processor not configured(最简单的解决办法)
    在使用@ConfigurationProperties是报红:SpringBootConfigurationAnnotationProcessornotconfigured,如下图所示:其实这个不影响程序运行,但作为程序员就是看着不舒服,网上也有解决办法,其中最多的就是说在pom.xml中加入以下依赖:<dependency><groupId>org.springframework......
  • mybatis-config.xml配置
    具体代码<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>......
  • LYT-C#-Socket——简单的TCP服务端-客户端连接通信
    https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.tcplistener?view=netframework-4.8.1TCP编程的服务器端一般步骤是:1、创建一个socket,用函数socket()。2、设置socket属性。3、绑定本机的IP地址、端口等信息到socket上,用函数bind()。......
  • ELEC3115 ENGINEERING
    ELEC3115–ELECTROMAGNETICENGINEERINGPartBassignment–T12023DueDate:23:59pm,Monday24thApril2020(Week11)AssignmentssubmittedaftertheDueDatewillbepenalizedbya20%marksreduction.CutoffDate:23:59pm,Tuesday25thApril2020(Week11)......