public string Print_Ticket(byte[] document) { string text = Find_Printer(); if (string.IsNullOrEmpty(text)) { return(null); } NativeMethods.DOC_INFO_1 dOC_INFO_ = new NativeMethods.DOC_INFO_1(); dOC_INFO_.pDataType = "RAW"; dOC_INFO_.pDocName = "Bit Image Test"; IntPtr hPrinter = new IntPtr(0); if (NativeMethods.OpenPrinter(text.Normalize(), out hPrinter, IntPtr.Zero)) { if (NativeMethods.StartDocPrinter(hPrinter, 1, dOC_INFO_)) { IntPtr intPtr = Marshal.AllocCoTaskMem(document.Length); Marshal.Copy(document, 0, intPtr, document.Length); if (NativeMethods.StartPagePrinter(hPrinter)) { NativeMethods.WritePrinter(hPrinter, intPtr, document.Length, out int _); NativeMethods.EndPagePrinter(hPrinter); Marshal.FreeCoTaskMem(intPtr); NativeMethods.EndDocPrinter(hPrinter); NativeMethods.ClosePrinter(hPrinter); return(text); } throw new Win32Exception(); } throw new Win32Exception(); } throw new Win32Exception(); }
public bool Print_ESCPOS(string printerName, byte[] document) { NativeMethods.DOC_INFO_1 dOC_INFO_ = new NativeMethods.DOC_INFO_1(); dOC_INFO_.pDataType = "RAW"; dOC_INFO_.pDocName = "Bit Image Test"; IntPtr hPrinter = new IntPtr(0); if (NativeMethods.OpenPrinter(printerName.Normalize(), out hPrinter, IntPtr.Zero)) { if (NativeMethods.StartDocPrinter(hPrinter, 1, dOC_INFO_)) { IntPtr intPtr = Marshal.AllocCoTaskMem(document.Length); Marshal.Copy(document, 0, intPtr, document.Length); if (NativeMethods.StartPagePrinter(hPrinter)) { NativeMethods.WritePrinter(hPrinter, intPtr, document.Length, out int _); NativeMethods.EndPagePrinter(hPrinter); Marshal.FreeCoTaskMem(intPtr); NativeMethods.EndDocPrinter(hPrinter); NativeMethods.ClosePrinter(hPrinter); return(true); } throw new Win32Exception(); } throw new Win32Exception(); } throw new Win32Exception(); }