C#调用斑马打印机打印条码标签(支持COM/LPT/USB/ZPL/EPL/Bitmap)



利用标签设计软件做好模板,打印至本地文件,把其中的ZPL、EPL指令拷贝出来,替换其中动态变化的内容为变量名,做成一个模板文本,在代码中动态替换变量,再把指令输出至打印机。



 

ZebraPrintHelper.cs。

 

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Drawing.Imaging;  
  5. using System.Drawing.Printing;  
  6. using System.IO;  
  7. using System.IO.Ports;  
  8. using System.Linq;  
  9. using System.Runtime.InteropServices;  
  10. using System.Text;  
  11. using Microsoft.Win32.SafeHandles;  
  12.   
  13. namespace Umisky.HTServer.Util  
  14. {  
  15.     #region 定义设备类型枚举  
  16.     public enum DeviceType  
  17.     {  
  18.         COM = 0,  
  19.         LPT = 1,  
  20.         DRV = 2  
  21.     }  
  22.     #endregion  
  23.  
  24.     #region 定义打印机指令类型枚举  
  25.     public enum ProgrammingLanguage  
  26.     {  
  27.         ZPL = 0,  
  28.         EPL = 1  
  29.     }  
  30.     #endregion  
  31.  
  32.     #region 定义日志类型枚举  
  33.     public enum LogType  
  34.     {  
  35.         Print = 0,  
  36.         Error = 1  
  37.     }  
  38.     #endregion  
  39.  
  40.     #region 定义打印文档信息类  
  41.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  42.     public class DocInfo  
  43.     {  
  44.         [MarshalAs(UnmanagedType.LPStr)]  
  45.         public string DocName;  
  46.         [MarshalAs(UnmanagedType.LPStr)]  
  47.         public string OutputFile;  
  48.         [MarshalAs(UnmanagedType.LPStr)]  
  49.         public string DataType;  
  50.     }  
  51.     #endregion  
  52.  
  53.     #region 定义图像设备信息类  
  54.     public class DeviceInfo  
  55.     {  
  56.         #region 属性说明  
  57.         /* 
  58.         ColorDepth  
  59.             图像输出支持的颜色范围的像素深度。有效值为 1、4、8、24 和 32。默认值为 24。仅对 TIFF 呈现支持 ColorDepth,对于其他图像输出格式报表服务器将忽略此设置。 
  60.  
  61.         注意:  
  62.         对于此版本的 SQL Server,此设置的值将被忽略,且通常将 TIFF 图像呈现为 24 位。 
  63.   
  64.         Columns  
  65.             要为报表设置的列数。此值将覆盖报表的原始设置。 
  66.   
  67.         ColumnSpacing  
  68.             要为报表设置的列间距。此值将覆盖报表的原始设置。 
  69.   
  70.         DpiX  
  71.             输出设备在 X 方向的分辨率。默认值为 96。 
  72.   
  73.         DpiY  
  74.             输出设备在 Y 方向的分辨率。默认值为 96。 
  75.   
  76.         EndPage  
  77.             要呈现的报表的最后一页。默认值为 StartPage 的值。 
  78.   
  79.         MarginBottom  
  80.             要为报表设置的下边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  81.   
  82.         MarginLeft  
  83.             要为报表设置的左边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  84.   
  85.         MarginRight  
  86.             要为报表设置的右边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  87.   
  88.         MarginTop  
  89.             要为报表设置的上边距值,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,1in)。此值将覆盖报表的原始设置。 
  90.   
  91.         OutputFormat  
  92.             图形设备接口 (GDI) 支持的输出格式之一:BMP、EMF、GIF、JPEG、PNG 或 TIFF。 
  93.   
  94.         PageHeight  
  95.             要为报表设置的页高,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,11in)。此值将覆盖报表的原始设置。 
  96.   
  97.         PageWidth  
  98.             要为报表设置的页宽,以英寸为单位。您必须包含一个整数或小数值,后跟“in”(例如,8.5in)。此值将覆盖报表的原始设置。 
  99.   
  100.         StartPage  
  101.             要呈现的报告的第一页。值为 0 指示将呈现所有页。默认值为 1。  
  102.          */  
  103.         #endregion  
  104.   
  105.         public enum GDIOutputFormat { BMP, EMF, GIF, JPEG, PNG, TIFF }  
  106.   
  107.         public int ColorDepth { getset; }  
  108.         public int Columns { getset; }  
  109.         public int ColumnSpacing { getset; }  
  110.         public int DpiX { getset; }  
  111.         public int DpiY { getset; }  
  112.         public int EndPage { getset; }  
  113.         public int MarginBottom { getset; }  
  114.         public int MarginLeft { getset; }  
  115.         public int MarginRight { getset; }  
  116.         public int MarginTop { getset; }  
  117.         public GDIOutputFormat OutputFormat { getset; }  
  118.         public int PageHeight { getset; }  
  119.         public int PageWidth { getset; }  
  120.         public int StartPage { getset; }  
  121.   
  122.         private const string xmlFormater = @"  
  123.                 {0}  
  124.                 {1}  
  125.                 {2}  
  126.                 {3}  
  127.                 {4}  
  128.                 {5}  
  129.                 {6}  
  130.                 {7}  
  131.                 {8}  
  132.                 {9}  
  133.                 {10}  
  134.                 {11}  
  135.                 {12}  
  136.                 {13}  
  137.                 ";  
  138.   
  139.         public DeviceInfo()  
  140.         {  
  141.             this.ColorDepth = 24;  
  142.             this.Columns = 0;  
  143.             this.StartPage = 1;  
  144.             this.EndPage = 1;  
  145.         }  
  146.   
  147.         public string GetDeviceInfo()  
  148.         {  
  149.             string result = string.Format(xmlFormater,  
  150.                 this.ColorDepth,  
  151.                 this.Columns,  
  152.                 this.ColumnSpacing,  
  153.                 this.DpiX,  
  154.                 this.DpiY,  
  155.                 this.EndPage,  
  156.                 this.MarginBottom,  
  157.                 this.MarginLeft,  
  158.                 this.MarginRight,  
  159.                 this.MarginTop,  
  160.                 this.OutputFormat,  
  161.                 this.PageHeight,  
  162.                 this.PageWidth,  
  163.                 this.StartPage);  
  164.             return result;  
  165.         }  
  166.   
  167.         public string GetDeviceInfoForImage()  
  168.         {  
  169.             string result = string.Format("{0}{1}{2}{3}{4}",  
  170.                 this.StartPage,  
  171.                 this.EndPage,  
  172.                 this.OutputFormat,  
  173.                 this.DpiX,  
  174.                 this.DpiY);  
  175.             return result;  
  176.         }  
  177.     }  
  178.     #endregion  
  179.  
  180.     #region 定义斑马打印助手类  
  181.     ///   
  182.     /// 斑马打印助手,支持LPT/COM/DRV三种模式,适用于标签、票据、条码打印。  
  183.     ///   
  184.     public static class ZebraPrintHelper  
  185.     {  
  186.         #region 定义API方法  
  187.  
  188.         #region 写打印口(LPT)方法  
  189.         private const short FILE_ATTRIBUTE_NORMAL = 0x80;  
  190.         private const short INVALID_HANDLE_VALUE = -1;  
  191.         private const uint GENERIC_READ = 0x80000000;  
  192.         private const uint GENERIC_WRITE = 0x40000000;  
  193.         private const uint CREATE_NEW = 1;  
  194.         private const uint CREATE_ALWAYS = 2;  
  195.         private const uint OPEN_EXISTING = 3;  
  196.         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  197.         private static extern SafeFileHandle CreateFile(string strFileName,  
  198.             uint dwDesiredAccess,  
  199.             uint dwShareMode,  
  200.             IntPtr intptrSecurityAttributes,  
  201.             uint dwCreationDisposition,  
  202.             uint dwFlagsAndAttributes,  
  203.             IntPtr intptrTemplateFile);  
  204.         #endregion  
  205.   
  206.         [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  207.         public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string printerName, out IntPtr intptrPrinter, IntPtr intptrPrintDocument);  
  208.   
  209.         [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  210.         public static extern bool ClosePrinter(IntPtr intptrPrinter);  
  211.   
  212.         [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  213.         public static extern bool StartDocPrinter(IntPtr intptrPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DocInfo docInfo);  
  214.   
  215.         [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  216.         public static extern bool EndDocPrinter(IntPtr intptrPrinter);  
  217.   
  218.         [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  219.         public static extern bool StartPagePrinter(IntPtr intptrPrinter);  
  220.   
  221.         [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  222.         public static extern bool EndPagePrinter(IntPtr intptrPrinter);  
  223.   
  224.         [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]  
  225.         public static extern bool WritePrinter(IntPtr intptrPrinter, IntPtr intptrBytes, Int32 count, out Int32 written);  
  226.         #endregion  
  227.  
  228.         #region 定义私有字段  
  229.   
  230.         ///   
  231.         /// 线程锁,防止多线程调用。  
  232.         ///   
  233.         private static object SyncRoot = new object();  
  234.   
  235.         ///   
  236.         /// 字节流传递时采用的字符编码  
  237.         ///   
  238.         private static readonly Encoding TransferFormat = Encoding.GetEncoding("iso-8859-1");  
  239.  
  240.         #endregion  
  241.  
  242.         #region 定义属性  
  243.         public static int Port { getset; }  
  244.         public static string PrinterName { getset; }  
  245.         public static bool IsWriteLog { getset; }  
  246.         public static DeviceType PrinterType { getset; }  
  247.         public static ProgrammingLanguage PrinterProgrammingLanguage { getset; }  
  248.   
  249.         ///   
  250.         /// 日志保存目录,WEB应用注意不能放在BIN目录下。  
  251.         ///   
  252.         public static string LogsDirectory { getset; }  
  253.   
  254.         private static byte[] GraphBuffer { getset; }  
  255.         private static int GraphWidth { getset; }  
  256.         private static int GraphHeight { getset; }  
  257.   
  258.         private static int RowSize  
  259.         {  
  260.             get  
  261.             {  
  262.                 return (((GraphWidth) + 31) >> 5) << 2;  
  263.             }  
  264.         }  
  265.   
  266.         private static int RowRealBytesCount  
  267.         {  
  268.             get  
  269.             {  
  270.                 if ((GraphWidth % 8) > 0)  
  271.                 {  
  272.                     return GraphWidth / 8 + 1;  
  273.                 }  
  274.                 else  
  275.                 {  
  276.                     return GraphWidth / 8;  
  277.                 }  
  278.             }  
  279.         }  
  280.         #endregion  
  281.  
  282.         #region 静态构造方法  
  283.         static ZebraPrintHelper()  
  284.         {  
  285.             GraphBuffer = new byte[0];  
  286.             IsWriteLog = false;  
  287.             LogsDirectory = "logs";  
  288.         }  
  289.         #endregion  
  290.  
  291.         #region 定义发送原始数据到打印机的方法  
  292.         private static bool SendBytesToPrinter(string printerName, IntPtr intptrBytes, Int32 count)  
  293.         {  
  294.             Int32 error = 0, written = 0;  
  295.             IntPtr intptrPrinter = new IntPtr(0);  
  296.             DocInfo docInfo = new DocInfo();  
  297.             bool bSuccess = false;  
  298.   
  299.             docInfo.DocName = ".NET RAW Document";  
  300.             docInfo.DataType = "RAW";  
  301.   
  302.             // Open the printer.  
  303.             if (OpenPrinter(printerName.Normalize(), out intptrPrinter, IntPtr.Zero))  
  304.             {  
  305.                 // Start a document.  
  306.                 if (StartDocPrinter(intptrPrinter, 1, docInfo))  
  307.                 {  
  308.                     // Start a page.  
  309.                     if (StartPagePrinter(intptrPrinter))  
  310.                     {  
  311.                         // Write your bytes.  
  312.                         bSuccess = WritePrinter(intptrPrinter, intptrBytes, count, out written);  
  313.                         EndPagePrinter(intptrPrinter);  
  314.                     }  
  315.                     EndDocPrinter(intptrPrinter);  
  316.                 }  
  317.                 ClosePrinter(intptrPrinter);  
  318.             }  
  319.             // If you did not succeed, GetLastError may give more information  
  320.             // about why not.  
  321.             if (bSuccess == false)  
  322.             {  
  323.                 error = Marshal.GetLastWin32Error();  
  324.             }  
  325.             return bSuccess;  
  326.         }  
  327.   
  328.         public static bool SendFileToPrinter(string printerName, string fileName)  
  329.         {  
  330.             // Open the file.  
  331.             FileStream fs = new FileStream(fileName, FileMode.Open);  
  332.             // Create a BinaryReader on the file.  
  333.             BinaryReader br = new BinaryReader(fs);  
  334.             // Dim an array of bytes big enough to hold the file's contents.  
  335.             Byte[] bytes = new Byte[fs.Length];  
  336.             bool bSuccess = false;  
  337.             // Your unmanaged pointer.  
  338.             IntPtr pUnmanagedBytes = new IntPtr(0);  
  339.             int nLength;  
  340.   
  341.             nLength = Convert.ToInt32(fs.Length);  
  342.             // Read the contents of the file into the array.  
  343.             bytes = br.ReadBytes(nLength);  
  344.             // Allocate some unmanaged memory for those bytes.  
  345.             pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);  
  346.             // Copy the managed byte array into the unmanaged array.  
  347.             Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);  
  348.             // Send the unmanaged bytes to the printer.  
  349.             bSuccess = SendBytesToPrinter(printerName, pUnmanagedBytes, nLength);  
  350.             // Free the unmanaged memory that you allocated earlier.  
  351.             Marshal.FreeCoTaskMem(pUnmanagedBytes);  
  352.             return bSuccess;  
  353.         }  
  354.   
  355.         public static bool SendBytesToPrinter(string printerName, byte[] bytes)  
  356.         {  
  357.             bool bSuccess = false;  
  358.             IntPtr pUnmanagedBytes = new IntPtr(0);  
  359.             int nLength = bytes.Length;  
  360.             // Allocate some unmanaged memory for those bytes.  
  361.             pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);  
  362.             // Copy the managed byte array into the unmanaged array.  
  363.             Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);  
  364.             // Send the unmanaged bytes to the printer.  
  365.             bSuccess = SendBytesToPrinter(printerName, pUnmanagedBytes, nLength);  
  366.             // Free the unmanaged memory that you allocated earlier.  
  367.             Marshal.FreeCoTaskMem(pUnmanagedBytes);  
  368.             return bSuccess;  
  369.         }  
  370.   
  371.         public static bool SendStringToPrinter(string printerName, string text)  
  372.         {  
  373.             IntPtr pBytes;  
  374.             Int32 dwCount;  
  375.             // How many characters are in the string?  
  376.             dwCount = (text.Length + 1) * Marshal.SystemMaxDBCSCharSize;  
  377.             // Assume that the printer is expecting ANSI text, and then convert  
  378.             // the string to ANSI text.  
  379.             pBytes = Marshal.StringToCoTaskMemAnsi(text);  
  380.             // Send the converted ANSI string to the printer.  
  381.             SendBytesToPrinter(printerName, pBytes, dwCount);  
  382.             Marshal.FreeCoTaskMem(pBytes);  
  383.             return true;  
  384.         }  
  385.         #endregion  
  386.  
  387.         #region 日志记录方法  
  388.         private static void WriteLog(string text, LogType logType)  
  389.         {  
  390.             string endTag = string.Format("\r\n{0}\r\n"new string('=', 80));  
  391.             string path = string.Format("{0}\\{1}-{2}.log", LogsDirectory, DateTime.Now.ToString("yyyy-MM-dd"), logType);  
  392.             if (!Directory.Exists(LogsDirectory))  
  393.             {  
  394.                 Directory.CreateDirectory(LogsDirectory);  
  395.             }  
  396.             if (logType == LogType.Error)  
  397.             {  
  398.                 File.AppendAllText(path, string.Format("{0}{1}", text, endTag), Encoding.Default);  
  399.             }  
  400.             if (logType == LogType.Print)  
  401.             {  
  402.                 if (text.StartsWith("N\r\nGW"))  
  403.                 {  
  404.                     using (FileStream fs = new FileStream(path, FileMode.Append))  
  405.                     {  
  406.                         byte[] bytes = TransferFormat.GetBytes(text);  
  407.                         byte[] tag = TransferFormat.GetBytes(endTag);  
  408.                         fs.Write(bytes, 0, bytes.Length);  
  409.                         fs.Write(tag, 0, tag.Length);  
  410.                         fs.Close();  
  411.                     }  
  412.                 }  
  413.                 else  
  414.                 {  
  415.                     File.AppendAllText(path, string.Format("{0}{1}", text, endTag), Encoding.Default);  
  416.                 }  
  417.             }  
  418.         }  
  419.   
  420.         private static void WriteLog(byte[] bytes, LogType logType)  
  421.         {  
  422.             string endTag = string.Format("\r\n{0}\r\n"new string('=', 80));  
  423.             string path = string.Format("{0}\\{1}-{2}.log", LogsDirectory, DateTime.Now.ToString("yyyy-MM-dd"), logType);  
  424.             if (!Directory.Exists(LogsDirectory))  
  425.             {  
  426.                 Directory.CreateDirectory(LogsDirectory);  
  427.             }  
  428.             if (logType == LogType.Error)  
  429.             {  
  430.                 File.AppendAllText(path, string.Format("{0}{1}", Encoding.Default.GetString(bytes), endTag), Encoding.Default);  
  431.             }  
  432.             if (logType == LogType.Print)  
  433.             {  
  434.                 string transferFormat = TransferFormat.GetString(bytes);  
  435.                 if (transferFormat.StartsWith("N\r\nGW"))  
  436.                 {  
  437.                     using (FileStream fs = new FileStream(path, FileMode.Append))  
  438.                     {  
  439.                         byte[] tag = TransferFormat.GetBytes(endTag);  
  440.                         fs.Write(bytes, 0, bytes.Length);  
  441.                         fs.Write(tag, 0, tag.Length);  
  442.                         fs.Close();  
  443.                     }  
  444.                 }  
  445.                 else  
  446.                 {  
  447.                     File.AppendAllText(path, string.Format("{0}{1}", Encoding.Default.GetString(bytes), endTag), Encoding.Default);  
  448.                 }  
  449.             }  
  450.         }  
  451.         #endregion  
  452.  
  453.         #region 封装方法,方便调用。  
  454.         public static bool PrintWithCOM(string cmd, int port, bool isWriteLog)  
  455.         {  
  456.             PrinterType = DeviceType.COM;  
  457.             Port = port;  
  458.             IsWriteLog = isWriteLog;  
  459.             return PrintCommand(cmd);  
  460.         }  
  461.   
  462.         public static bool PrintWithCOM(byte[] bytes, int port, bool isWriteLog, ProgrammingLanguage progLanguage)  
  463.         {  
  464.             PrinterType = DeviceType.COM;  
  465.             Port = port;  
  466.             IsWriteLog = isWriteLog;  
  467.             PrinterProgrammingLanguage = progLanguage;  
  468.             return PrintGraphics(bytes);  
  469.         }  
  470.   
  471.         public static bool PrintWithLPT(string cmd, int port, bool isWriteLog)  
  472.         {  
  473.             PrinterType = DeviceType.LPT;  
  474.             Port = port;  
  475.             IsWriteLog = isWriteLog;  
  476.             return PrintCommand(cmd);  
  477.         }  
  478.   
  479.         public static bool PrintWithLPT(byte[] bytes, int port, bool isWriteLog, ProgrammingLanguage progLanguage)  
  480.         {  
  481.             PrinterType = DeviceType.LPT;  
  482.             Port = port;  
  483.             IsWriteLog = isWriteLog;  
  484.             PrinterProgrammingLanguage = progLanguage;  
  485.             return PrintGraphics(bytes);  
  486.         }  
  487.   
  488.         public static bool PrintWithDRV(string cmd, string printerName, bool isWriteLog)  
  489.         {  
  490.             PrinterType = DeviceType.DRV;  
  491.             PrinterName = printerName;  
  492.             IsWriteLog = isWriteLog;  
  493.             return PrintCommand(cmd);  
  494.         }  
  495.   
  496.         public static bool PrintWithDRV(byte[] bytes, string printerName, bool isWriteLog, ProgrammingLanguage progLanguage)  
  497.         {  
  498.             PrinterType = DeviceType.DRV;  
  499.             PrinterName = printerName;  
  500.             IsWriteLog = isWriteLog;  
  501.             PrinterProgrammingLanguage = progLanguage;  
  502.             return PrintGraphics(bytes);  
  503.         }  
  504.         #endregion  
  505.  
  506.         #region 打印ZPL、EPL指令  
  507.         public static bool PrintCommand(string cmd)  
  508.         {  
  509.             lock (SyncRoot)  
  510.             {  
  511.                 bool result = false;  
  512.                 try  
  513.                 {  
  514.                     switch (PrinterType)  
  515.                     {  
  516.                         case DeviceType.COM:  
  517.                             result = comPrint(Encoding.Default.GetBytes(cmd));  
  518.                             break;  
  519.                         case DeviceType.LPT:  
  520.                             result = lptPrint(Encoding.Default.GetBytes(cmd));  
  521.                             break;  
  522.                         case DeviceType.DRV:  
  523.                             result = drvPrint(Encoding.Default.GetBytes(cmd));  
  524.                             break;  
  525.                     }  
  526.                     if (!string.IsNullOrEmpty(cmd) && IsWriteLog)  
  527.                     {  
  528.                         WriteLog(cmd, LogType.Print);  
  529.                     }  
  530.                 }  
  531.                 catch (Exception ex)  
  532.                 {  
  533.                     //记录日志  
  534.                     if (IsWriteLog)  
  535.                     {  
  536.                         WriteLog(string.Format("{0} => {1}\r\n{2}", DateTime.Now, ex.Message, ex), LogType.Error);  
  537.                     }  
  538.                 }  
  539.                 finally  
  540.                 {  
  541.                     GraphBuffer = new byte[0];  
  542.                 }  
  543.                 return result;  
  544.             }  
  545.         }  
  546.         #endregion  
  547.  
  548.         #region 打印图像字节流  
  549.         public static bool PrintGraphics(byte[] graph)  
  550.         {  
  551.             lock (SyncRoot)  
  552.             {  
  553.                 bool result = false;  
  554.                 try  
  555.                 {  
  556.                     GraphBuffer = graph;  
  557.                     byte[] cmdBytes = new byte[0];  
  558.                     if (PrinterProgrammingLanguage == ProgrammingLanguage.ZPL)  
  559.                     {  
  560.                         cmdBytes = getZPLBytes();  
  561.                     }  
  562.                     if (PrinterProgrammingLanguage == ProgrammingLanguage.EPL)  
  563.                     {  
  564.                         cmdBytes = getEPLBytes();  
  565.                     }  
  566.                     switch (PrinterType)  
  567.                     {  
  568.                         case DeviceType.COM:  
  569.                             result = comPrint(cmdBytes);  
  570.                             break;  
  571.                         case DeviceType.LPT:  
  572.                             result = lptPrint(cmdBytes);  
  573.                             break;  
  574.                         case DeviceType.DRV:  
  575.                             result = drvPrint(cmdBytes);  
  576.                             break;  
  577.                     }  
  578.                     if (cmdBytes.Length > 0 && IsWriteLog)  
  579.                     {  
  580.                         WriteLog(cmdBytes, LogType.Print);  
  581.                     }  
  582.                 }  
  583.                 catch (Exception ex)  
  584.                 {  
  585.                     //记录日志  
  586.                     if (IsWriteLog)  
  587.                     {  
  588.                         WriteLog(string.Format("{0} => {1}\r\n{2}", DateTime.Now, ex.Message, ex), LogType.Error);  
  589.                     }  
  590.                 }  
  591.                 finally  
  592.                 {  
  593.                     GraphBuffer = new byte[0];  
  594.                 }  
  595.                 return result;  
  596.             }  
  597.         }  
  598.         #endregion  
  599.  
  600.         #region COM/LPT/DRV三种模式打印方法  
  601.         private static bool drvPrint(byte[] cmdBytes)  
  602.         {  
  603.             bool result = false;  
  604.             try  
  605.             {  
  606.                 if (!string.IsNullOrEmpty(PrinterName))  
  607.                 {  
  608.                     result = SendBytesToPrinter(PrinterName, cmdBytes);  
  609.                 }  
  610.             }  
  611.             catch (Exception ex)  
  612.             {  
  613.                 throw ex;  
  614.             }  
  615.             return result;  
  616.         }  
  617.   
  618.         private static bool comPrint(byte[] cmdBytes)  
  619.         {  
  620.             bool result = false;  
  621.             SerialPort com = new SerialPort(string.Format("{0}{1}", PrinterType, Port), 9600, Parity.None, 8, StopBits.One);  
  622.             try  
  623.             {  
  624.                 com.Open();  
  625.                 com.Write(cmdBytes, 0, cmdBytes.Length);  
  626.                 result = true;  
  627.             }  
  628.             catch (Exception ex)  
  629.             {  
  630.                 throw ex;  
  631.             }  
  632.             finally  
  633.             {  
  634.                 if (com.IsOpen)  
  635.                 {  
  636.                     com.Close();  
  637.                 }  
  638.             }  
  639.             return result;  
  640.         }  
  641.   
  642.         private static bool lptPrint(byte[] cmdBytes)  
  643.         {  
  644.             bool result = false;  
  645.             FileStream fileStream = null;  
  646.             StreamWriter streamWriter = null;  
  647.             SafeFileHandle handle = null;  
  648.             try  
  649.             {  
  650.                 handle = CreateFile(string.Format("{0}{1}", PrinterType, Port), GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);  
  651.                 if (!handle.IsInvalid)  
  652.                 {  
  653.                     fileStream = new FileStream(handle, FileAccess.ReadWrite);  
  654.                     streamWriter = new StreamWriter(fileStream, Encoding.Default);  
  655.                     streamWriter.Write(cmdBytes);  
  656.                     result = true;  
  657.                 }  
  658.             }  
  659.             catch (Exception ex)  
  660.             {  
  661.                 throw ex;  
  662.             }  
  663.             finally  
  664.             {  
  665.                 if (fileStream != null)  
  666.                 {  
  667.                     fileStream.Close();  
  668.                     fileStream = null;  
  669.                 }  
  670.                 if (streamWriter != null)  
  671.                 {  
  672.                     streamWriter.Close();  
  673.                     streamWriter = null;  
  674.                 }  
  675.                 if (handle != null)  
  676.                 {  
  677.                     handle.Close();  
  678.                     handle = null;  
  679.                 }  
  680.             }  
  681.             return result;  
  682.         }  
  683.         #endregion  
  684.  
  685.         #region 生成ZPL图像打印指令  
  686.         private static byte[] getZPLBytes()  
  687.         {  
  688.             byte[] result = new byte[0];  
  689.             byte[] bmpData = getBitmapData();  
  690.             string textBitmap = string.Empty;  
  691.             string textHex = BitConverter.ToString(bmpData).Replace("-"string.Empty);  
  692.             for (int i = 0; i < GraphHeight;i++ )  
  693.             {  
  694.                 textBitmap += textHex.Substring(i * RowRealBytesCount * 2, RowRealBytesCount * 2) + "\r\n";  
  695.             }  
  696.             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",   
  697.                 GraphHeight * RowRealBytesCount,   
  698.                 RowRealBytesCount,   
  699.                 textBitmap);  
  700.             result = Encoding.Default.GetBytes(text);  
  701.             return result;  
  702.         }  
  703.         #endregion  
  704.  
  705.         #region 生成EPL图像打印指令  
  706.         private static byte[] getEPLBytes()  
  707.         {  
  708.             byte[] result = new byte[0];  
  709.             byte[] buffer = getBitmapData();  
  710.             string text = string.Format("N\r\nGW{0},{1},{2},{3},{4}\r\nP\r\n",   
  711.                 0,   
  712.                 0,   
  713.                 RowRealBytesCount,   
  714.                 GraphHeight,   
  715.                 TransferFormat.GetString(buffer));  
  716.             result = TransferFormat.GetBytes(text);  
  717.             return result;  
  718.         }  
  719.         #endregion  
  720.  
  721.         #region 获取单色位图数据  
  722.         ///   
  723.         /// 获取单色位图数据(1bpp),不含文件头、信息头、调色板三类数据。  
  724.         ///   
  725.         ///   
  726.         private static byte[] getBitmapData()  
  727.         {  
  728.             MemoryStream srcStream = new MemoryStream();  
  729.             MemoryStream dstStream = new MemoryStream();  
  730.             Bitmap srcBmp = null;  
  731.             Bitmap dstBmp = null;  
  732.             byte[] srcBuffer = null;  
  733.             byte[] dstBuffer = null;  
  734.             byte[] result = null;  
  735.             try  
  736.             {  
  737.                 srcStream = new MemoryStream(GraphBuffer);  
  738.                 srcBmp = Bitmap.FromStream(srcStream) as Bitmap;  
  739.                 srcBuffer = srcStream.ToArray();  
  740.                 GraphWidth = srcBmp.Width;  
  741.                 GraphHeight = srcBmp.Height;  
  742.                 dstBmp = srcBmp.Clone(new Rectangle(0, 0, srcBmp.Width, srcBmp.Height), PixelFormat.Format1bppIndexed);  
  743.                 dstBmp.Save(dstStream, ImageFormat.Bmp);  
  744.                 dstBuffer = dstStream.ToArray();  
  745.   
  746.                 int bfSize = BitConverter.ToInt32(dstBuffer, 2);  
  747.                 int bfOffBits = BitConverter.ToInt32(dstBuffer, 10);  
  748.                 int bitmapDataLength = bfSize - bfOffBits;  
  749.                 result = new byte[GraphHeight * RowRealBytesCount];  
  750.   
  751.                 //读取时需要反向读取每行字节实现上下翻转的效果,打印机打印顺序需要这样读取。  
  752.                 for (int i = 0; i < GraphHeight; i++)  
  753.                 {  
  754.                     Array.Copy(dstBuffer, bfOffBits + (GraphHeight - 1 - i) * RowSize, result, i * RowRealBytesCount, RowRealBytesCount);  
  755.                 }  
  756.             }  
  757.             catch (Exception ex)  
  758.             {  
  759.                 throw ex;  
  760.             }  
  761.             finally  
  762.             {  
  763.                 if (srcStream != null)  
  764.                 {  
  765.                     srcStream.Dispose();  
  766.                     srcStream = null;  
  767.                 }  
  768.                 if (dstStream != null)  
  769.                 {  
  770.                     dstStream.Dispose();  
  771.                     dstStream = null;  
  772.                 }  
  773.                 if (srcBmp != null)  
  774.                 {  
  775.                     srcBmp.Dispose();  
  776.                     srcBmp = null;  
  777.                 }  
  778.                 if (dstBmp != null)  
  779.                 {  
  780.                     dstBmp.Dispose();  
  781.                     dstBmp = null;  
  782.                 }  
  783.             }  
  784.             return result;  
  785.         }  
  786.         #endregion  
  787.     }  
  788.     #endregion  
  789. }  


 如何获取标签设计软件输出至打印的ZPL指令?

安装好打印机驱动,修改打印机端口,新建一个打印机端口,类型为本地端口,端口名称设置为C:\printer.log,再用标签设计软件打印一次,此文件中就有ZPL指令了。

你可能感兴趣的:(C#调用斑马打印机打印条码标签(支持COM/LPT/USB/ZPL/EPL/Bitmap))