PrintHelper.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Text;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using ZXing;
  11. using ZXing.QrCode;
  12. namespace UAS_Web.tool
  13. {
  14. class PrintHelper
  15. {
  16. // 结构和API declarions
  17. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  18. public class DOCINFOA
  19. {
  20. [MarshalAs(UnmanagedType.LPStr)]
  21. public string pDocName;
  22. [MarshalAs(UnmanagedType.LPStr)]
  23. public string pOutputFile;
  24. [MarshalAs(UnmanagedType.LPStr)]
  25. public string pDataType;
  26. }
  27. [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  28. public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
  29. [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  30. public static extern bool ClosePrinter(IntPtr hPrinter);
  31. [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  32. public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  33. [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  34. public static extern bool EndDocPrinter(IntPtr hPrinter);
  35. [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  36. public static extern bool StartPagePrinter(IntPtr hPrinter);
  37. [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  38. public static extern bool EndPagePrinter(IntPtr hPrinter);
  39. [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  40. public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  41. // SendBytesToPrinter()
  42. //当函数是给定一个打印机名称和一个非托管数组
  43. // 如果执行成功,将返回true;如果执行失败将返回false
  44. public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  45. {
  46. Int32 dwError = 0, dwWritten = 0;
  47. IntPtr hPrinter = new IntPtr(0);
  48. DOCINFOA di = new DOCINFOA();
  49. bool bSuccess = false; // 假设失败.
  50. di.pDocName = "My C#.NET RAW Document";
  51. di.pDataType = "RAW";
  52. // 打开打印机
  53. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  54. {
  55. // 开始一个文档.
  56. if (StartDocPrinter(hPrinter, 1, di))
  57. {
  58. // 创建新页面
  59. if (StartPagePrinter(hPrinter))
  60. {
  61. // 写你的字节
  62. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  63. EndPagePrinter(hPrinter);
  64. }
  65. EndDocPrinter(hPrinter);
  66. }
  67. ClosePrinter(hPrinter);
  68. }
  69. // 如果没有成功,可能会给更多的信息GetLastError
  70. if (bSuccess == false)
  71. {
  72. dwError = Marshal.GetLastWin32Error();
  73. }
  74. return bSuccess;
  75. }
  76. public static bool SendFileToPrinter(string szPrinterName, string szFileName)
  77. {
  78. // 打开文件.
  79. FileStream fs = new FileStream(szFileName, FileMode.Open);
  80. // 创建一个主题的文件.
  81. BinaryReader br = new BinaryReader(fs);
  82. // .
  83. Byte[] bytes = new Byte[fs.Length];
  84. bool bSuccess = false;
  85. // 非托管指针
  86. IntPtr pUnmanagedBytes = new IntPtr(0);
  87. int nLength;
  88. nLength = Convert.ToInt32(fs.Length);
  89. // 读取文件的内容放入数组中.
  90. bytes = br.ReadBytes(nLength);
  91. // 分配一些非托管内存对于那些字节
  92. pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  93. // 复制管理到非托管数组的字节数组
  94. Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
  95. // 发送到打印机的非托管的字节
  96. bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
  97. // 非托管内存分配
  98. Marshal.FreeCoTaskMem(pUnmanagedBytes);
  99. return bSuccess;
  100. }
  101. public static bool SendStringToPrinter(string szPrinterName, string szString)
  102. {
  103. IntPtr pBytes;
  104. Int32 dwCount;
  105. // 有多少字符在字符串
  106. dwCount = szString.Length;
  107. // 假设打印机期待ANSI的文本,然后将字符串ANSI文本。
  108. pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  109. // 转换后的字符串发送到打印机的ANSI
  110. SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  111. Marshal.FreeCoTaskMem(pBytes);
  112. return true;
  113. }
  114. //图片添加文字
  115. public static void Printstring(string str, int size, Graphics g, double x, double y)
  116. {
  117. //g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
  118. //g.InterpolationMode = InterpolationMode.HighQualityBilinear;
  119. //g.CompositingQuality = CompositingQuality.HighQuality;
  120. g.CompositingQuality = CompositingQuality.HighQuality;
  121. g.SmoothingMode = SmoothingMode.AntiAlias;
  122. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  123. g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
  124. g.PixelOffsetMode = PixelOffsetMode.Half;
  125. //Font font = new Font(new FontFamily("宋体"), (float)(siez*1.3), FontStyle.Regular);
  126. Font font = new Font(new FontFamily("微软雅黑"), (float)(size*1.2), FontStyle.Regular, GraphicsUnit.Point);
  127. g.DrawString(str, font, Brushes.Black, new PointF((float)x * 4, (float)y * 4));
  128. }
  129. //图片添加条码
  130. public static void DrawBarcode(string str, ref Bitmap bitmap, double x, double y, double width, double height)
  131. {
  132. QrCodeEncodingOptions options = new QrCodeEncodingOptions();
  133. options.Margin = 1;
  134. options.Width = (int)width * 4;
  135. options.Height = (int)height * 4;
  136. options.PureBarcode = true;
  137. BarcodeWriter writer = new BarcodeWriter();
  138. writer.Format = BarcodeFormat.CODE_128;
  139. writer.Options = options;
  140. Bitmap imgTemp = writer.Write(str);
  141. //BarCode _Code = new BarCode();
  142. //_Code.ValueFont = new Font(new FontFamily("黑体"), (float)(6 * 25.4 /18), FontStyle.Regular);
  143. //_Code.Height = 6*4;
  144. //Bitmap imgTemp = _Code.GetCodeImage("0012490202261807040001",
  145. //BarCode.Encode.Code128A);
  146. bitmap = (Bitmap)CombinImage(bitmap, imgTemp, (float)x, (float)y);
  147. }
  148. /// <summary>
  149. /// 调用此函数后使此两种图片合并,类似相册,有个
  150. /// 背景图,中间贴自己的目标图片
  151. /// </summary>
  152. /// <param name="imgBack">粘贴的源图片</param>
  153. /// <param name="img">粘贴的目标图片</param>
  154. public static Image CombinImage(Image imgBack, Image img, float x, float y)
  155. { //照片图片
  156. //从指定的System.Drawing.Image创建新的System.Drawing.Graphics
  157. Graphics g = Graphics.FromImage(imgBack);
  158. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  159. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  160. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  161. g.PixelOffsetMode = PixelOffsetMode.Half;
  162. //g.DrawImage(imgBack, 0, 0, 148, 124); // g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
  163. //g.FillRectangle(System.Drawing.Brushes.Black, -50, -50, (int)212, ((int)203));//相片四周刷一层黑色边框,这里没有,需要调尺寸
  164. //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
  165. g.DrawImage(img, x * 2-5, y * 4, img.Width, img.Height);
  166. g.Save();
  167. g.Dispose();
  168. return imgBack;
  169. }
  170. }
  171. }