统一调用打印机打印图片

  

本文实例为大家分享了统一打印机打印图片的具体代码,供大家参考,具体内容如下

  

<强> 1,调用打印机首先就是要配置好打印机
  

  

就是电脑跟打印机已经连接好,有默认的打印机可以启动使用
  

  

<强> 2,调用方式
  

  

(1)使用外部第三方软件exe
  

  

代码如下:就两句)

        字符串路径=应用程序。dataPath公司+ @“\纹理\ 002. png”;   System.Diagnostics.Process.Start (“mspaint。exe”,路径);//调用第三方应用去打印(其中路径是要打印图片的路径,而mspaint.exe是调用Windows中的画板,然后从画板里启用打印功能)      

(2)使用赢得自带软件
  

  

这个需要下载一个应用(应用会放在我的博客下载文件中名字是PrintImage.exe)
  然后直接上代码:

        公共空白测试()   {   字符串路径=应用程序。dataPath公司+ @”\纹理\ 002. png, 0, 0750400”;//从纸张的0。0点,将图像调整为750×350点(计算:150毫米/28.346 px/厘米=529点,100毫米/28.346点/厘米=352点)图片路径   字符串exepath=应用程序。streamingAssetsPath + @“\ PrintImage.exe”;//这个是需要下载的应用直接放到电脑上就行(调用打印机打印图片应用的路径)   ProcessStartInfo信息=new ProcessStartInfo (exepath);//指定启动进程时使用的一组值   信息。参数=路径;//获取或设置启动应用程序时要使用的一组命令行自变量   使用(p=新工艺过程())   {   p。StartInfo=信息;   p.Start ();   }   }      

(3)自己进行打印

     ///& lt; summary>///打?//& lt;/summary>   公共空间PrintFile ()   {   PrintDocument pri=new PrintDocument ();   pri.PrintPage +=Printpagetest;   pri.Print ();   }      私人空间Printpagetest(对象发送方,PrintPageEventArgs e)   {   试一试   {   System.Drawing。图像图像=System.Drawing.Image.FromFile (printPath);   System.Drawing。图g=e.Graphics;   g。TranslateTransform (_4AHeight 0);   g.RotateTransform (90);   g。DrawImage(图片0 0 _4AWidth _4AHeight);   }   捕获(异常ee)   {   Debug.LogError (ee.Message);   }   }      

(这里的第三种我还未进行测试,如出现错误无法实现请指正)
  

  

这里我选择的是第二种,1不好实现静默,3太麻烦,2使用是后台调用命令行
  

  

<强> 3,颜色问题

  

同时这里本人还找到了有博主自己写的调用打印机方法
  项目中需要用到调用打印机打印图片,原本觉得会很复杂,结果一搜索发现Assetstore有相应的插件。在网上找到别人分享的插件,完美的实现了功能,所以现在也来分享一下(因为想看到具体实现,所以用工具反编译了DLL,原本插件是直接导入就可以的)。

        使用系统;   使用System.Diagnostics;   使用System.Drawing.Printing;   使用先;   使用UnityEngine;      名称空间LCPrinter   {   公共静态类打印   {   公共静态孔隙PrintTexture (byte [] texture2DBytes, int numCopies,字符串printerName)   {   如果(texture2DBytes==null)   {   UnityEngine.Debug。LogWarning (“LCPrinter:纹理是空的。”);   返回;   }   PrinterSettings PrinterSettings=new PrinterSettings ();   如果(printerName==null | | printerName.Equals (" "))   {   printerName=printerSettings.PrinterName;   UnityEngine.Debug。日志(“LCPrinter:打印默认打印机(“+ printerName +”)。”);   }   字符串str=字符串。Concat(新string []   {   DateTime.Now.Year.ToString (),   “-”,   DateTime.Now.Month.ToString (),   “-”,   DateTime.Now.Day.ToString (),   “-”,   DateTime.Now.Hour.ToString (),   “-”,   DateTime.Now.Minute.ToString (),   “-”,   DateTime.Now.Second.ToString (),   “-”,   DateTime.Now.Millisecond.ToString ()   });   字符串文字=(应用程序。persistentDataPath +“\ \ LCPrinterFiletmp_”+ str + . png)。替换(“/薄ⅰ癨 \”);   UnityEngine.Debug。日志(“LCPrinter:临时路径——“+文本);   文件。WriteAllBytes(文本、texture2DBytes);   打印。PrintCMD(文本、numCopies printerName);   }      公共静态孔隙PrintTextureByPath字符串(字符串路径、int numCopies printerName)   {   PrinterSettings PrinterSettings=new PrinterSettings ();   如果(printerName==null | | printerName.Equals (" "))   {   printerName=printerSettings.PrinterName;   UnityEngine.Debug。日志(“LCPrinter:打印默认打印机(“+ printerName +”)。”);   }   打印。PrintCMD(路径、numCopies printerName);   }      私有静态孔隙PrintCMD(字符串路径、int numCopies字符串printerName)   {   过程过程=new过程();   试一试   {   for (int i=0;我& lt;numCopies;我+ +)   {   process.StartInfo。文件名=" rundll32”;   process.StartInfo。参数=字符串。Concat(新string []   {   “C: \ \ WINDOWS \ \ system32系统\ \ shimgvw。dll, ImageView_PrintTo \ ",   路径,   “\ \”,   printerName,   “\”“   });   process.StartInfo。WindowStyle=ProcessWindowStyle.Hidden;   process.StartInfo。UseShellExecute=true;   process.Start ();   }   }   捕获(异常参数)   {   UnityEngine.Debug。LogWarning (arg“LCPrinter: " +);   }   最后   {   process.Close ();   UnityEngine.Debug。日志(“LCPrinter:纹理印刷。”);   }   }   }   }

统一调用打印机打印图片