Unity調(diào)用打印機打印圖片
本文實例為大家分享了Unity打印機打印圖片的具體代碼,供大家參考,具體內(nèi)容如下
1、調(diào)用打印機首先就是要配置好打印機
就是電腦跟打印機已經(jī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自帶軟件
這個需要下載一個應(yīng)用(應(yīng)用會放在我的博客下載文件中名字是PrintImage.exe)
然后直接上代碼:
public void Test()
{
string path = Application.dataPath + @"\Textures\002.png,0,0,750,400";//從紙張的0. 0點,將圖像調(diào)整為750×350點(計算:150mm/28.346 px/cm=529點,100mm/28.346 pm/cm=352點) 圖片路徑
string exepath = Application.streamingAssetsPath + @"\PrintImage.exe";//這個是需要下載的應(yīng)用直接放到電腦上就行(調(diào)用打印機打印圖片應(yīng)用的路徑)
ProcessStartInfo info = new ProcessStartInfo(exepath);//指定啟動進程時使用的一組值
info.Arguments = path;//獲取或設(shè)置啟動應(yīng)用程序時要使用的一組命令行自變量
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);
}
}
(這里的第三種我還未進行測試,如出現(xiàn)錯誤無法實現(xiàn)請指正)
這里我選擇的是第二種,1不好實現(xiàn)靜默,3太麻煩,2使用是后臺調(diào)用命令行
3、顏色問題
同時這里本人還找到了有博主自己寫的調(diào)用打印機方法
項目中需要用到調(diào)用打印機打印圖片,原本覺得會很復雜,結(jié)果一搜索發(fā)現(xiàn)Assetstore有相應(yīng)的插件。在網(wǎng)上找到別人分享的插件,完美的實現(xiàn)了功能,所以現(xiàn)在也來分享一下(因為想看到具體實現(xiàn),所以用工具反編譯了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.");
}
}
}
}
這是實現(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程序程序放入項目中的插件下。如在VS中報錯沒有添加引用,則要對項目添加引用
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
newtonsoft.json解析天氣數(shù)據(jù)出錯解決方法
這篇文章主要介紹了NewtonSoft.JSon解析天氣數(shù)據(jù)時出錯的解決方法,需要的朋友可以參考下2014-02-02
C#實現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限
這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-03-03

