本文實(shí)例為大家分享了Unity打印機(jī)打印圖片的具體代碼,供大家參考,具體內(nèi)容如下
站在用戶的角度思考問題,與客戶深入溝通,找到淅川網(wǎng)站設(shè)計(jì)與淅川網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋淅川地區(qū)。
1、調(diào)用打印機(jī)首先就是要配置好打印機(jī)
就是電腦跟打印機(jī)已經(jīng)連接好,有默認(rèn)的打印機(jī)可以啟動(dòng)使用
2、調(diào)用方式
(1)使用外部第三方軟件exe
代碼如下:(就兩句)
string path = Application.dataPath + @"\Textures\002.png"; System.Diagnostics.Process.Start("mspaint.exe", path);//調(diào)用第三方應(yīng)用去打印(其中path是要打印圖片的路徑,而mspaint.exe是調(diào)用Windows中的畫板,然后從畫板里啟用打印功能)
(2)使用win自帶軟件
這個(gè)需要下載一個(gè)應(yīng)用(應(yīng)用會(huì)放在我的博客下載文件中名字是PrintImage.exe)
然后直接上代碼:
public void Test() { string path = Application.dataPath + @"\Textures\002.png,0,0,750,400";//從紙張的0. 0點(diǎn),將圖像調(diào)整為750×350點(diǎn)(計(jì)算:150mm/28.346 px/cm=529點(diǎn),100mm/28.346 pm/cm=352點(diǎn)) 圖片路徑 string exepath = Application.streamingAssetsPath + @"\PrintImage.exe";//這個(gè)是需要下載的應(yīng)用直接放到電腦上就行(調(diào)用打印機(jī)打印圖片應(yīng)用的路徑) ProcessStartInfo info = new ProcessStartInfo(exepath);//指定啟動(dòng)進(jìn)程時(shí)使用的一組值 info.Arguments = path;//獲取或設(shè)置啟動(dòng)應(yīng)用程序時(shí)要使用的一組命令行自變量 using (Process p=new Process()) { p.StartInfo = info; p.Start(); } }
(3)自己進(jìn)行打印
////// 打印 /// public void PrintFile() { PrintDocument pri = new PrintDocument(); pri.PrintPage += Printpagetest; pri.Print(); } private void Printpagetest(object sender, PrintPageEventArgs e) { try { System.Drawing.Image image = System.Drawing.Image.FromFile(printPath); System.Drawing.Graphics g = e.Graphics; g.TranslateTransform(_4AHeight, 0); g.RotateTransform(90); g.DrawImage(image, 0, 0, _4AWidth, _4AHeight); } catch (Exception ee) { Debug.LogError(ee.Message); } }
(這里的第三種我還未進(jìn)行測試,如出現(xiàn)錯(cuò)誤無法實(shí)現(xiàn)請(qǐng)指正)
這里我選擇的是第二種,1不好實(shí)現(xiàn)靜默,3太麻煩,2使用是后臺(tái)調(diào)用命令行
3、顏色問題
同時(shí)這里本人還找到了有博主自己寫的調(diào)用打印機(jī)方法
項(xiàng)目中需要用到調(diào)用打印機(jī)打印圖片,原本覺得會(huì)很復(fù)雜,結(jié)果一搜索發(fā)現(xiàn)Assetstore有相應(yīng)的插件。在網(wǎng)上找到別人分享的插件,完美的實(shí)現(xiàn)了功能,所以現(xiàn)在也來分享一下(因?yàn)橄肟吹骄唧w實(shí)現(xiàn),所以用工具反編譯了DLL,原本插件是直接導(dǎo)入就可以的)。
using System; using System.Diagnostics; using System.Drawing.Printing; using System.IO; using UnityEngine; namespace LCPrinter { public static class Print { public static void PrintTexture(byte[] texture2DBytes, int numCopies, string printerName) { if (texture2DBytes == null) { UnityEngine.Debug.LogWarning("LCPrinter: Texture is empty."); return; } PrinterSettings printerSettings = new PrinterSettings(); if (printerName == null || printerName.Equals("")) { printerName = printerSettings.PrinterName; UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ")."); } string str = string.Concat(new 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() }); string text = (Application.persistentDataPath + "\\LCPrinterFiletmp_" + str + ".png").Replace("/", "\\"); UnityEngine.Debug.Log("LCPrinter: Temporary Path - " + text); File.WriteAllBytes(text, texture2DBytes); Print.PrintCMD(text, numCopies, printerName); } public static void PrintTextureByPath(string path, int numCopies, string printerName) { PrinterSettings printerSettings = new PrinterSettings(); if (printerName == null || printerName.Equals("")) { printerName = printerSettings.PrinterName; UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ")."); } Print.PrintCMD(path, numCopies, printerName); } private static void PrintCMD(string path, int numCopies, string printerName) { Process process = new Process(); try { for (int i = 0; i < numCopies; i++) { process.StartInfo.FileName = "rundll32"; process.StartInfo.Arguments = string.Concat(new string[] { "C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_PrintTo \"", path, "\" \"", printerName, "\"" }); process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.UseShellExecute = true; process.Start(); } } catch (Exception arg) { UnityEngine.Debug.LogWarning("LCPrinter: " + arg); } finally { process.Close(); UnityEngine.Debug.Log("LCPrinter: Texture printing."); } } } }
這是實(shí)現(xiàn)功能的源碼。調(diào)用方法如下:
using UnityEngine; using System.Collections; using System.Diagnostics; using System; using System.IO; using LCPrinter; using UnityEngine.UI; public class LCExampleScript : MonoBehaviour { public Texture2D texture2D; public string printerName = ""; public int copies = 1; public InputField inputField; public void printSmileButton() { Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);//打印一張編輯器中的圖片 } public void printByPathButton() { Print.PrintTextureByPath("D:\\pic.png", copies, printerName);//打印一張存在指定路徑的圖片 } }
由于原本插件是添加好引用的,反編譯之后缺少了引用,所以要去統(tǒng)一的安裝路徑E:\ unity5.3.2 \統(tǒng)一\編輯\數(shù)據(jù)\單聲道\ lib中\(zhòng)單\ 2.0(這是我本地安裝的路徑)中找到System.Drawing.dll程序程序放入項(xiàng)目中的插件下。如在VS中報(bào)錯(cuò)沒有添加引用,則要對(duì)項(xiàng)目添加引用
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。