国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity調(diào)用打印機(jī)打印圖片

2020-01-23 20:47:05
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Unity打印機(jī)打印圖片的具體代碼,供大家參考,具體內(nèi)容如下

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)行打印

/// <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);    }  }

(這里的第三種我還未進(jìn)行測試,如出現(xiàn)錯(cuò)誤無法實(shí)現(xiàn)請指正)

這里我選擇的是第二種,1不好實(shí)現(xiàn)靜默,3太麻煩,2使用是后臺調(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 + "http://LCPrinterFiletmp_" + str + ".png").Replace("/", "http://");      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中/單/ 2.0(這是我本地安裝的路徑)中找到System.Drawing.dll程序程序放入項(xiàng)目中的插件下。如在VS中報(bào)錯(cuò)沒有添加引用,則要對項(xiàng)目添加引用

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 保亭| 蒲城县| 孝感市| 贵德县| 密山市| 克什克腾旗| 星座| 平阳县| 巩义市| 定南县| 微博| 乳山市| 长子县| 宜丰县| 麻江县| 拜城县| 探索| 博白县| 蒙山县| 禹州市| 青州市| 乌鲁木齐县| 阿克陶县| 清水县| 宿迁市| 鱼台县| 铁力市| 兴安县| 桃园市| 新安县| 金昌市| 宁波市| 五大连池市| 呼伦贝尔市| 安溪县| 江北区| 上蔡县| 双城市| 金塔县| 宜都市| 太保市|