您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Unity中怎么調用打印機打印圖片,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1、調用打印機首先就是要配置好打印機
就是電腦跟打印機已經連接好,有默認的打印機可以啟動使用
2、調用方式
(1)使用外部第三方軟件exe
代碼如下:(就兩句)
string path = Application.dataPath + @"\Textures02.png"; System.Diagnostics.Process.Start("mspaint.exe", path);//調用第三方應用去打印(其中path是要打印圖片的路徑,而mspaint.exe是調用Windows中的畫板,然后從畫板里啟用打印功能)
(2)使用win自帶軟件
這個需要下載一個應用(應用會放在我的博客下載文件中名字是PrintImage.exe) 然后直接上代碼:
public void Test() { string path = Application.dataPath + @"\Textures02.png,0,0,750,400";//從紙張的0. 0點,將圖像調整為750×350點(計算:150mm/28.346 px/cm=529點,100mm/28.346 pm/cm=352點) 圖片路徑 string exepath = Application.streamingAssetsPath + @"\PrintImage.exe";//這個是需要下載的應用直接放到電腦上就行(調用打印機打印圖片應用的路徑) ProcessStartInfo info = new ProcessStartInfo(exepath);//指定啟動進程時使用的一組值 info.Arguments = path;//獲取或設置啟動應用程序時要使用的一組命令行自變量 using (Process p=new Process()) { p.StartInfo = info; p.Start(); } }
(3)自己進行打印
/// <summary> /// 打印 /// </summary> 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); } }
(這里的第三種我還未進行測試,如出現錯誤無法實現請指正)
這里我選擇的是第二種,1不好實現靜默,3太麻煩,2使用是后臺調用命令行
3、顏色問題
同時這里本人還找到了有博主自己寫的調用打印機方法項目中需要用到調用打印機打印圖片,原本覺得會很復雜,結果一搜索發現Assetstore有相應的插件。在網上找到別人分享的插件,完美的實現了功能,所以現在也來分享一下(因為想看到具體實現,所以用工具反編譯了DLL,原本插件是直接導入就可以的)。
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."); } } }}
這是實現功能的源碼。調用方法如下:
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);//打印一張存在指定路徑的圖片 }}
上述就是小編為大家分享的Unity中怎么調用打印機打印圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。