Unity调用打印机打印图片

作者:月夜风雨磊 时间:2023-10-18 03:49:48 

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

1、调用打印机首先就是要配置好打印机

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

2、调用方式

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

代码如下:(就两句)


string path = Application.dataPath + @"\Textures\002.png";
 System.Diagnostics.Process.Start("mspaint.exe", path);//调用第三方应用去打印(其中path是要打印图片的路径,而mspaint.exe是调用Windows中的画板,然后从画板里启用打印功能)

(2)使用win自带软件

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


public void Test()
 {
   string path = Application.dataPath + @"\Textures\002.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);//打印一张存在指定路径的图片
 }
}

由于原本插件是添加好引用的,反编译之后缺少了引用,所以要去统一的安装路径E:\ unity5.3.2 \统一\编辑\数据\单声道\ lib中\单\ 2.0(这是我本地安装的路径)中找到System.Drawing.dll程序程序放入项目中的插件下。如在VS中报错没有添加引用,则要对项目添加引用

来源:https://blog.csdn.net/qq_42855293/article/details/81981308

标签:Unity,打印机,打印
0
投稿

猜你喜欢

  • DevExpress GridView自动滚动效果

    2022-01-19 09:08:54
  • java中获取json的所有key方法

    2023-10-15 06:15:26
  • struts2中实现多个文件同时上传代码

    2023-05-11 16:24:39
  • Java 实战项目之毕业设计管理系统的实现流程

    2021-12-26 00:10:10
  • java使用回溯法求解数独示例

    2023-08-17 14:39:10
  • Android实现过渡动画、引导页 Android判断是否第一次启动App

    2023-09-08 06:08:11
  • Android仿QQ6.0主页面侧滑效果

    2022-08-06 03:44:16
  • Java 八种基本类型和基本类型封装类

    2023-11-26 14:15:16
  • Unity 修改FBX模型动画的操作

    2023-03-17 17:59:39
  • 一文快速掌握Spring Cloud Stream

    2023-09-01 23:09:38
  • C# 类的声明详解

    2022-10-15 14:08:38
  • c#文件的复制,移动,创建(实例代码)

    2023-05-29 21:49:14
  • java抓包后对pcap文件解析示例

    2022-11-23 20:21:53
  • java常用工具类之DES和Base64加密解密类

    2023-12-20 17:48:31
  • C#词法分析器之构造NFA详解

    2022-04-10 02:11:43
  • Java中finally和return的关系实例解析

    2023-05-31 04:21:31
  • Spring Cloud Eureka 注册与发现操作步骤详解

    2022-06-25 07:11:12
  • C#实现文件上传及文件下载功能实例代码

    2022-12-13 23:57:23
  • 关于QueryWrapper,实现MybatisPlus多表关联查询方式

    2021-10-15 07:24:59
  • Java开发druid数据连接池maven方式简易配置流程示例

    2021-05-26 14:57:23
  • asp之家 软件编程 m.aspxhome.com