【Silverlight5矢量打印】如何用C#代码检测打印机和驱动是否支持PostScript
2012-08-26 11:08 slmk 阅读(1155) 评论(0) 编辑 收藏 举报Silverlight5支持PostScript矢量打印,矢量打印相比于位图打印速度更快,生成的打印文件更小。SL5默认会采用PS矢量打印,如果打印机不支持,自动切换到位图打印。
虽然微软SL打印组认为PS已经相当普遍,但我想大多数打印机估计并未安装支持PostScript的驱动。HP打印机默认的会是PCL的,估计微软选用PS是学Flash:)
如何用代码检测打印机是否支持PostScript呢?代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Runtime.InteropServices; namespace PrinterSupportPS { class Program { static void Main(string[] args) { string printerName = "Microsoft XPS Document Writer"; Console.WriteLine(PrinterSupportsPostScript(printerName)); Console.Read(); } static Int32 GETTECHNOLOGY = 20; static Int32 QUERYESCSUPPORT = 8; static Int32 POSTSCRIPT_PASSTHROUGH = 4115; static Int32 ENCAPSULATED_POSTSCRIPT = 4116; static Int32 POSTSCRIPT_IDENTIFY = 4117; static Int32 POSTSCRIPT_INJECTION = 4118; static Int32 POSTSCRIPT_DATA = 37; static Int32 POSTSCRIPT_IGNORE = 38; [DllImport("gdi32.dll")] static extern int ExtEscape(IntPtr hdc, int nEscape, int cbInput, IntPtr lpszInData, int cbOutput, IntPtr lpszOutData); [DllImport("gdi32.dll")] static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, IntPtr lpszOutput, IntPtr lpInitData); [DllImport("gdi32.dll")] static extern bool DeleteDC(IntPtr hdc); static bool PrinterSupportsPostScript(string printername) { ArrayList PSChecks = new ArrayList(); PSChecks.Add(POSTSCRIPT_PASSTHROUGH); PSChecks.Add(ENCAPSULATED_POSTSCRIPT); PSChecks.Add(POSTSCRIPT_IDENTIFY); PSChecks.Add(POSTSCRIPT_INJECTION); PSChecks.Add(POSTSCRIPT_DATA); PSChecks.Add(POSTSCRIPT_IGNORE); IntPtr hDC = IntPtr.Zero; ; IntPtr BLOB = IntPtr.Zero; try { hDC = CreateDC(null, printername, IntPtr.Zero, IntPtr.Zero); int isz = 4; BLOB = Marshal.AllocCoTaskMem(isz); Marshal.WriteInt32(BLOB, GETTECHNOLOGY); int test = ExtEscape(hDC, QUERYESCSUPPORT, 4, BLOB, 0, IntPtr.Zero); if (test == 0) return false; // printer driver does not support GETTECHNOLOGY Checks. foreach (Int32 val in PSChecks) { Marshal.WriteInt32(BLOB, val); test = ExtEscape(hDC, QUERYESCSUPPORT, isz, BLOB, 0, IntPtr.Zero); if (test != 0) return true; // if any of the checks pass, return true } } catch (Exception ex) { Console.WriteLine(ex); } finally { if (hDC != IntPtr.Zero) DeleteDC(hDC); if (BLOB != IntPtr.Zero) Marshal.Release(BLOB); }; return false; } } }
大家可以建一个控制台程序,用这段代码检测一下。