123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace UAS_PRINT
- {
- class PrintHelper
- {
-
-
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
- public class DOCINFOA
- {
- [MarshalAs(UnmanagedType.LPStr)]
- public string pDocName;
- [MarshalAs(UnmanagedType.LPStr)]
- public string pOutputFile;
- [MarshalAs(UnmanagedType.LPStr)]
- public string pDataType;
- }
- [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
- [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool ClosePrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
- [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool EndDocPrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool StartPagePrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool EndPagePrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
-
-
-
- public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
- {
- Int32 dwError = 0, dwWritten = 0;
- IntPtr hPrinter = new IntPtr(0);
- DOCINFOA di = new DOCINFOA();
- bool bSuccess = false;
- di.pDocName = "My C#.NET RAW Document";
- di.pDataType = "RAW";
-
- if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
- {
-
- if (StartDocPrinter(hPrinter, 1, di))
- {
-
- if (StartPagePrinter(hPrinter))
- {
-
- bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
- EndPagePrinter(hPrinter);
- }
- EndDocPrinter(hPrinter);
- }
- ClosePrinter(hPrinter);
- }
-
- if (bSuccess == false)
- {
- dwError = Marshal.GetLastWin32Error();
- }
- return bSuccess;
- }
- public static bool SendFileToPrinter(string szPrinterName, string szFileName)
- {
-
- FileStream fs = new FileStream(szFileName, FileMode.Open);
-
- BinaryReader br = new BinaryReader(fs);
-
- Byte[] bytes = new Byte[fs.Length];
- bool bSuccess = false;
-
- IntPtr pUnmanagedBytes = new IntPtr(0);
- int nLength;
- nLength = Convert.ToInt32(fs.Length);
-
- bytes = br.ReadBytes(nLength);
-
- pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
-
- Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
-
- bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
-
- Marshal.FreeCoTaskMem(pUnmanagedBytes);
- return bSuccess;
- }
- public static bool SendStringToPrinter(string szPrinterName, string szString)
- {
- IntPtr pBytes;
- Int32 dwCount;
-
- dwCount = szString.Length;
-
- pBytes = Marshal.StringToCoTaskMemAnsi(szString);
-
- SendBytesToPrinter(szPrinterName, pBytes, dwCount);
- Marshal.FreeCoTaskMem(pBytes);
- return true;
- }
-
- public static void Printstring(string str, int size, Graphics g, double x, double y)
- {
-
-
-
- g.CompositingQuality = CompositingQuality.HighQuality;
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
- g.PixelOffsetMode = PixelOffsetMode.Half;
-
- Font font = new Font(new FontFamily("微软雅黑"), (float)(size*1.2), FontStyle.Regular, GraphicsUnit.Point);
- g.DrawString(str, font, Brushes.Black, new PointF((float)x * 4, (float)y * 4));
- }
-
-
-
-
-
-
- public static Image CombinImage(Image imgBack, Image img, float x, float y)
- {
-
- Graphics g = Graphics.FromImage(imgBack);
- g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
- g.PixelOffsetMode = PixelOffsetMode.Half;
-
-
-
- g.DrawImage(img,5, y * 4, img.Width, img.Height);
- g.Save();
- g.Dispose();
- return imgBack;
- }
- public static Dictionary<string, object> ToDictionary(string JsonData)
- {
- object Data = null;
- Dictionary<string, object> Dic = new Dictionary<string, object>();
- if (JsonData.StartsWith("["))
- {
-
-
- List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
- MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");
- foreach (Match ListItem in ListMatch)
- {
- List.Add(ToDictionary(ListItem.ToString()));
- }
- Data = List;
- Dic.Add("List", Data);
- }
- else
- {
- MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\}\s*\]|null|"".+?""(,+?)|"".+?""(\s*?)(\}+?)|-{0,1}\d*)");
- foreach (Match item in Match)
- {
- try
- {
- if (item.Groups[2].ToString().StartsWith("["))
- {
-
-
- List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
- MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");
- foreach (Match ListItem in ListMatch)
- {
- List.Add(ToDictionary(ListItem.ToString()));
- }
- Data = List;
- }
- else if (item.Groups[2].ToString().ToLower() == "null") Data = null;
- else if (item.Groups[2].ToString().EndsWith(","))
- Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1);
- else if (item.Groups[2].ToString().EndsWith("}"))
- Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1);
- else
- Data = item.Groups[2].ToString();
- Dic.Add(item.Groups[1].ToString(), Data);
- }
- catch { }
- }
- }
- return Dic;
- }
- }
- }
|