使用 WinApiPrintByte("打印机名",byte[]数据) 就可以了
/// <summary>
/// 发送数据到打印机
/// zgke@sina.com
/// qq:116149
/// </summary>
/// <param name="p_PrintName">打印机</param>
/// <param name="p_Byte">数据</param>
public static void WinApiPrintByte(string p_PrintName, byte[] p_Byte)
{
if (p_PrintName != null && p_PrintName.Length > 0)
{
IntPtr _PrintHandle;
IntPtr _JobHandle = Marshal.AllocHGlobal(100);
if (OpenPrinter(p_PrintName, out _PrintHandle, IntPtr.Zero))
{
ADDJOB_INFO_1 _JobInfo = new ADDJOB_INFO_1();
int _Size;
AddJob(_PrintHandle, 1, _JobHandle, 100, out _Size);
_JobInfo = (ADDJOB_INFO_1)Marshal.PtrToStructure(_JobHandle, typeof(ADDJOB_INFO_1));
//System.IO.File.WriteAllBytes(p_PrintName, p_Byte);
System.IO.File.WriteAllBytes(_JobInfo.lpPath, p_Byte);
ScheduleJob(_PrintHandle, _JobInfo.JobID);
ClosePrinter(_PrintHandle);
Marshal.FreeHGlobal(_JobHandle);
}
}
}
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)]string strPrinterName, out IntPtr ptrPrinter, IntPtr ptrDefalut);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool ClosePrinter(IntPtr ptrPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool AddJob(IntPtr ptrPrinter, Int32 iLevel, IntPtr ptrJob, Int32 iSize, out Int32 iCpSize);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool ScheduleJob(IntPtr ptrPrinter, Int32 JobID);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ADDJOB_INFO_1
{
[MarshalAs(UnmanagedType.LPTStr)]
public string lpPath;
public Int32 JobID;
}