private void btnPrinter_Click(object sender, EventArgs e)
{ #region ESC 热敏图像点阵像素点读取打印 //Bitmap bitmap = new Bitmap(@"D:\450X100.bmp");
//NetPOSPrinter netPOSPrinter = new NetPOSPrinter();
//netPOSPrinter.PrintPic(bitmap); #endregion #region EPL USB 打印
//Bitmap img = new Bitmap(@"D:\450X100.bmp");
//ZebraPrintHelper.PrinterType = DeviceType.DRV;
//ZebraPrintHelper.PrinterProgrammingLanguage = ProgrammingLanguage.EPL;
//ZebraPrintHelper.PrinterName = "ZDesigner GK888t (EPL)";
//byte[] imgByte = ImageToByte.ImgToByt(img);
//ZebraPrintHelper.PrintGraphics(imgByte); #endregion #region ZPL II 打印指令
//^XA^CFD
//^POI
//^LH330,10
//^FO50,50
//^FDSMARTDIGITAL^FS
//^FO50,75
//^FD021-51871630^FS
//^XZ //StringBuilder sb = new StringBuilder();
//sb.Append("^XA^CFD");
//sb.Append("^POI");
//sb.Append("^LH330,10");
//sb.Append("^FO50,50");
//sb.Append("^FDSMARTDIGITAL^FS");
//sb.Append("^FO50,75");
//sb.Append("^FD021-51871630^FS");
//sb.Append("^XZ"); //byte[] cmd = Encoding.Default.GetBytes(sb.ToString()); #endregion #region EPL USB 指令打印
ZebraPrintHelper.PrinterProgrammingLanguage = ProgrammingLanguage.EPL;
ZebraPrintHelper.PrinterName = "ZDesigner GK888t (EPL)";
ZebraPrintHelper.PrinterType = DeviceType.DRV; string cmd = "N" + "\r\n" +
"Q400,025" + "\n\r" +
"A140,45,0,8,1,1,N,\"古典黄芥沫调味酱\"" + "\r\n" +
"A140,90,0,8,1,1,N,\"规格:\"" + "\r\n" +
"A240,95,0,4,1,1,N,\"" + "100ML/瓶" + "\"" + "\r\n" +
"A140,135,0,8,1,1,N,\"生产日期:\"" + "\r\n" +
"A300,140,0,4,1,1,N,\"" + "2015-10-02" + "\"" + "\r\n" +
"B140,180,0,1,3,2,100,B,\"" + "" + "\"" + "\r\n" +
"P1" + "\r\n"; ZebraPrintHelper.PrintCommand(cmd.ToString());
#endregion
}
 using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text; namespace ZebraLibrary
{
/// <summary>
/// 斑马打印助手,支持LPT/COM/USB/TCP四种模式,适用于标签、票据、条码打印。
/// </summary>
public static class ZebraPrintHelper
{
#region 定义API方法 #region 写打印口(LPT)方法
private const short FILE_ATTRIBUTE_NORMAL = 0x80;
private const short INVALID_HANDLE_VALUE = -;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint CREATE_NEW = ;
private const uint CREATE_ALWAYS = ;
private const uint OPEN_EXISTING = ;
[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 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; } public static float TcpLabelMaxHeightCM { get; set; }
public static int TcpPrinterDPI { get; set; }
public static string TcpIpAddress { get; set; }
public static int TcpPort { get; set; }
public static int Copies { 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) + ) >> ) << ;
}
} private static int RowRealBytesCount
{
get
{
if ((GraphWidth % ) > )
{
return GraphWidth / + ;
}
else
{
return GraphWidth / ;
}
}
}
#endregion #region 静态构造方法
static ZebraPrintHelper()
{
GraphBuffer = new byte[];
IsWriteLog = false;
LogsDirectory = "logs";
}
#endregion #region 定义发送原始数据到打印机的方法
private static bool SendBytesToPrinter(string printerName, IntPtr intptrBytes, Int32 count)
{
Int32 error = , written = ;
IntPtr intptrPrinter = new IntPtr();
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, , 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();
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, , 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();
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, , 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 + ) * 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('=', ));
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, , bytes.Length);
fs.Write(tag, , 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('=', ));
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, , bytes.Length);
fs.Write(tag, , 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 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指令
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);
}
}
finally
{
GraphBuffer = new byte[];
}
return result;
}
}
#endregion #region 打印图像字节流
public static bool PrintGraphics(byte[] graph)
{
lock (SyncRoot)
{
bool result = false;
try
{
GraphBuffer = graph;
byte[] cmdBytes = new byte[];
if (PrinterProgrammingLanguage == ProgrammingLanguage.ZPL)
{
cmdBytes = getZPLBytes();
}
if (PrinterProgrammingLanguage == ProgrammingLanguage.EPL)
{
cmdBytes = getEPLBytes();
}
if (PrinterProgrammingLanguage == ProgrammingLanguage.CPCL)
{
cmdBytes = getCPCLBytes();
}
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 > && 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[];
}
return result;
}
}
#endregion #region COM/LPT/DRV三种模式打印方法
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), , Parity.None, , StopBits.One);
try
{
com.Open();
com.Write(cmdBytes, , 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, , IntPtr.Zero, OPEN_EXISTING, , 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
{
IPAddress ip = IPAddress.Parse(TcpIpAddress);
IPEndPoint iport = new IPEndPoint(ip, TcpPort);//9100为小票打印机指定端口
tcp = new TcpClient(iport);
tcp.SendTimeout = ;
tcp.ReceiveTimeout = ;
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[];
byte[] bmpData = getBitmapData();
string textBitmap = string.Empty;
string textHex = BitConverter.ToString(bmpData).Replace("-", string.Empty);
for (int i = ; i < GraphHeight; i++)
{
textBitmap += textHex.Substring(i * RowRealBytesCount * , RowRealBytesCount * ) + "\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[];
byte[] buffer = getBitmapData();
string text = string.Format("N\r\nGW{0},{1},{2},{3},{4}\r\nP\r\n",
,
,
RowRealBytesCount,
GraphHeight,
TransferFormat.GetString(buffer));
result = TransferFormat.GetBytes(text);
return result;
}
#endregion #region 生成CPCL图像打印指令
public static byte[] getCPCLBytes()
{
//GRAPHICS Commands
//Bit-mapped graphics can be printed by using graphics commands. ASCII hex (hexadecimal) is
//used for expanded graphics data (see example). Data size can be reduced to one-half by utilizing the
//COMPRESSED-GRAPHICS commands with the equivalent binary character(s) of the hex data. When
//using CG, a single 8 bit character is sent for every 8 bits of graphics data. When using EG two characters
//(16 bits) are used to transmit 8 bits of graphics data, making EG only half as efficient. Since this data is
//character data, however, it can be easier to handle and transmit than binary data.
//Format:
//{command} {width} {height} {x} {y} {data}
//where:
//{command}: Choose from the following:
//EXPANDED-GRAPHICS (or EG): Prints expanded graphics horizontally.
//VEXPANDED-GRAPHICS (or VEG): Prints expanded graphics vertically.
//COMPRESSED-GRAPHICS (or CG): Prints compressed graphics horizontally.
//VCOMPRESSED-GRAPHICS (or VCG): Prints compressed graphics vertically.
//{width}: Byte-width of image.
//{height} Dot-height of image.
//{x}: Horizontal starting position.
//{y}: Vertical starting position.
//{data}: Graphics data.
//Graphics command example
//Input:
//! 0 200 200 210 1
//EG 2 16 90 45 F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F
//F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F
//FORM
//PRINT byte[] bmpData = getBitmapData();
int bmpDataLength = bmpData.Length;
for (int i = ; i < bmpDataLength; i++)
{
bmpData[i] ^= 0xFF;
}
string textHex = BitConverter.ToString(bmpData).Replace("-", string.Empty);
string text = string.Format("! {0} {1} {2} {3} {4}\r\nEG {5} {6} {7} {8} {9}\r\nFORM\r\nPRINT\r\n",
, //水平偏移量
TcpPrinterDPI, //横向DPI
TcpPrinterDPI, //纵向DPI
(int)(TcpLabelMaxHeightCM / 2.54f * TcpPrinterDPI), //标签最大像素高度=DPI*标签纸高度(英寸)
Copies, //份数
RowRealBytesCount, //图像的字节宽度
GraphHeight, //图像的像素高度
, //横向的开始位置
, //纵向的开始位置
textHex
);
return Encoding.UTF8.GetBytes(text);
}
#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(, , srcBmp.Width, srcBmp.Height), PixelFormat.Format1bppIndexed);
dstBmp.Save(dstStream, ImageFormat.Bmp);
dstBuffer = dstStream.ToArray(); int bfSize = BitConverter.ToInt32(dstBuffer, );
int bfOffBits = BitConverter.ToInt32(dstBuffer, );
int bitmapDataLength = bfSize - bfOffBits;
result = new byte[GraphHeight * RowRealBytesCount]; //读取时需要反向读取每行字节实现上下翻转的效果,打印机打印顺序需要这样读取。
for (int i = ; i < GraphHeight; i++)
{
Array.Copy(dstBuffer, bfOffBits + (GraphHeight - - 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 #region 扩展 #region CPCL命令打印24位bmp
private static string get24BitBmpData(string filePath)
{
Bitmap bmp = new Bitmap(filePath);
byte[] bitArray = { , , , , , , , };
string imgTxt = "";
Rectangle rect = new Rectangle(, , bmp.Width, bmp.Height);
BitmapData data = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr firstPix = data.Scan0; int rowByteCount = bmp.Width * ;
int filledCount = data.Stride - rowByteCount;
int bytes = data.Stride * data.Height;//Math.Ceiling((double)bmp.Width / 8)
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(firstPix, rgbValues, , bytes); int printRowByteCount = Convert.ToInt32(Math.Ceiling((double)(bmp.Width) / ));
int printRowByteFillCount = - (printRowByteCount % );
//int bitFillCount = 8 - (bmp.Width % 8);
byte[] printData = new byte[(printRowByteCount + printRowByteFillCount) * bmp.Height]; int byteCount = ;
int bitCount = ;
int rowPoint = ;
for (int i = ; i < rgbValues.Length; i += )
{
int rgbValue = rgbValues[i] + rgbValues[i + ] + rgbValues[i + ];
if (rgbValue != ( * ))
{
printData[byteCount] = Convert.ToByte(printData[byteCount] | bitArray[bitCount]);
}
if (bitCount == )
{
bitCount = ;
byteCount++;
}
else
{
bitCount++;
}
if ((rowPoint + ) == rowByteCount)
{
rowPoint = ;
if (bitCount > )
{
byteCount++;
}
bitCount = ;
byteCount += printRowByteFillCount;
i = i + filledCount;
}
else
{
rowPoint += ;
}
} foreach (byte byteData in printData)
{
string hexStr = Convert.ToString(byteData, );
if (hexStr.Length == )
{
hexStr = '' + hexStr;
}
imgTxt += hexStr;
}
bmp.UnlockBits(data);
return imgTxt.ToUpper();
} public static void Printer(string bitbmpPath)
{
string CRNL = "\r\n";
string imgTxt = get24BitBmpData(bitbmpPath);
string cmddata = "! 0 200 200 300 1" + CRNL +
"EG " + + " " + + " 10 10 " + imgTxt + CRNL +
"FORM" + CRNL +
"PRINT" + CRNL;
try
{
string ipAddress = "192.168.1.212";
int port = ; // Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port); // Write CPCL String to connection
System.IO.StreamWriter writer =new System.IO.StreamWriter(client.GetStream());
writer.Write(cmddata);
writer.Flush(); // Close Connection
writer.Close();
client.Close();
}
catch (Exception)
{
// Catch Exception
}
}
#endregion #endregion
} #region 定义设备类型枚举
public enum DeviceType
{
COM = ,
LPT = ,
DRV = ,
TCP =
}
#endregion #region 定义打印机指令类型枚举
public enum ProgrammingLanguage
{
ZPL = ,
EPL = ,
CPCL =
}
#endregion #region 定义日志类型枚举
public enum LogType
{
Print = ,
Error =
}
#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 = ;
this.Columns = ;
this.StartPage = ;
this.EndPage = ;
} 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
}

http://www.cnblogs.com/rinack/p/4843627.html

C# EPL USB 指令打印的更多相关文章

  1. usb口打印机的指令打印和驱动打印

    打印机简介:是计算机的输出设备之一,用于将计算机处理结果打印在相关介质上. 打印机类型:激光打印机.喷墨打印机.针式打印机.热敏打印机等. 计算机和打印机之间的连接方式:usb口.串口.并口.网口.蓝 ...

  2. c# 调用zebra打印指令 打印到USB端口

    c# 调用zebra打印机指令打印条码,如果直接打印到lpt1端口的打印机,通过copy指令没有问题, 但如果ZEBRA打印机是通过USB连接,打印机端口为usb001,则程序不能直接拷贝到usb00 ...

  3. EPL II 编程打印

    一.EPL II 格式及打印测试 注意N命令前的换行和最后P1后的换行.将此段代码复制到windows记事本里另存为Print.ext,文件名随便,后缀为ext.然后通过cmd控制命令行输入" ...

  4. C# 网络打印机ESC指令打印小票

    public void SendSocketMsg(String ip, int port, int times, byte[] data) { try { byte[] mData; ) { mDa ...

  5. C# ZPL II 命令打印标签

    public class BarPrinter { public static byte[] ZPLPrinter(string p_title = "", string p_sp ...

  6. 吉特仓库管理系统-.NET打印问题总结

    在仓储系统的是使用过程中避免不了的是打印单据,仓库系统中包含很多单据:入库单,出库单,盘点单,调拨单,签收单等等,而且还附带着很多的条码标签的打印.本文在此记录一下一个简单的打印问题处理方式.处理问题 ...

  7. Linux Shell 脚本学习第一天: 使用grep 命令,lsusb, ps -ef, 实现树莓派(Debian OS)时检测到依赖的USB设备启动后,启动终端自动执行shell脚本

    1.应用背景: 无人监测的设备,常需要设置应用程序开机启动,程序启动前需要保证调用的设备先启动,运行环境先启动. 2.test.sh部分源码 #!/bin/sh #查看桌面是否启动 while tru ...

  8. 基于C#在WPF中使用斑马打印机进行打印【转】

    原文链接:http://ju.outofmemory.cn/entry/132476 最近在项目中接手了一个比较有挑战性的模块——用斑马打印机将需要打印的内容打印出来.苦苦折腾了两天,总算有所收获,就 ...

  9. (九)打印机驱动设置—USB接口的设置

    佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...

随机推荐

  1. 浅谈WEB跨域的实现(前端向)

    同源策略/SOP(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,浏览器很容易受到XSS.CSFR等攻击(可以参考我的这篇文章). SOP要求 ...

  2. Spring:源码解读Spring IOC原理

    Spring IOC设计原理解析:本文乃学习整理参考而来 一. 什么是Ioc/DI? 二. Spring IOC体系结构 (1) BeanFactory (2) BeanDefinition 三. I ...

  3. 分享自己写的JS版日期格式化和解析工具类,绝对好用!

    前言 本来想模仿Java里面的SimpleDateFormat()对象的,但是感觉这样用起来不方便,所以还是直接写成单独的方法算了. 原文链接 日期格式化 使用说明 formatDate(date, ...

  4. 小型文件数据库 (a file database for small apps) SharpFileDB

    小型文件数据库 (a file database for small apps) SharpFileDB For english version of this article, please cli ...

  5. 谁占了我的端口 for Windows

    这篇文章发布于我的 github 博客:原文 今天在本地调试 Blog 的时候意外的出现了一些错误:127.0.0.1 4000 端口已经被其他的进程占用了.如何找到占用端口的进程呢? Configu ...

  6. C语言 · 特殊回文数

    问题描述 123321是一个非常特殊的数,它从左边读和从右边读是一样的. 输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n . 输入格式 输入一行,包含一个正整数n. 输 ...

  7. ASP.NET Web API自身对CORS的支持:从实例开始

    在<通过扩展让ASP.NET Web API支持W3C的CORS规范>中我们通过自定义的HttpMessageHandler为ASP.NET Web API赋予了跨域资源共享的能力,具体来 ...

  8. WCF 安全性 之 Windows

    案例下载 http://download.csdn.net/detail/woxpp/4113172 服务端配置代码 <system.serviceModel> <services& ...

  9. WebDriver--操控浏览器

    前一篇讲述了元素的定位方法,现在开始练习如何写自动化测试脚本 我使用的编辑工具是PyCharm,今后该博客中所写的有关Python脚本,都是在该工具中编写的. WebDriver提供了控制浏览器大小. ...

  10. Chart.js中文文档-雷达图

    雷达图或蛛网图(Radar chart) 简介 A radar chart is a way of showing multiple data points and the variation bet ...