【问题标题】:How to direct printing of photo or text using Unity without preview如何在没有预览的情况下使用 Unity 直接打印照片或文本
【发布时间】:2018-03-09 07:05:40
【问题描述】:

我是Unity的初学者,我想直接将图像打印到连接的默认打印机而不预览。

我正在使用此代码进行打印,但需要预览

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class PrintImage : MonoBehaviour
{

public void PrintFile()
{
    PrintFiles();
}

void PrintFiles(string path=null)
{

    path = "file:///C:/Users/ersai/Desktop/2.jpg";
    System.Diagnostics.Process process = new System.Diagnostics.Process(); 
    process.StartInfo.CreateNoWindow = false; 
    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.FileName = path; 
    process.StartInfo.Verb = "print";

    process.Start(); 

  }
 }

这不是重复的,因为这不是窗口标签问题,我问的是关于 C# unity 的问题,即标记重复的问题不适用于 C# Unity。我被 LSPrinter Simple 从统一的评估商店解决了。

【问题讨论】:

  • CreatNoWindow 变量应该设置为true 而不是false...
  • Print images c#.net的可能重复
  • @ChuckSavage 不是真正的重复。该问题的有效答案使用 Windows 窗体 API 中的PrintDocument,这将使此代码仅适用于 Windows,并且此问题中没有 windows 标签。
  • Thankyou Programmer CreatBiWindo to true not working,这不是重复的问题,它不是关于统一的窗口标签问题,但 ChuckSavag 感谢您的评论,我使用资产商店中的 LSPrinter 解决了它很有用
  • @Programmer 他正在使用进程,关于使用进程的问题还有另一个答案。这是一个重复的 imo,但 OP 想出了一个统一的解决方案,所以这可能会更好。

标签: c# unit-testing unity3d


【解决方案1】:

我使用Ls Printer解决

如果printerName 为空或null,它将打印到您的默认打印机。

目前,它适用于 Windows。每台打印机都应该工作。

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 the texture2d using on
    // Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);*
    Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);
}

public void printByPathButton()
{
   //direct path which fill in inputfield
    Print.PrintTextureByPath(inputField.text.Trim(), copies, printerName);
}
}

需要一个小预览

System.Diagnostics.Process.Start("mspaint.exe", "/pt Assets\\Resources\\"+files);

【讨论】:

    【解决方案2】:

    可能我迟到了,但以下代码似乎在 Unity 2018 中对我有用

    void Start()
    {
        string printerName = "Canon TS8100 series";
        string _filePath = "C:\\ImagesFolder" + "\\1.jpg";
        string fullCommand = "rundll32 C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_PrintTo " + "\"" + _filePath + "\"" + " " + "\"" + printerName + "\"";
        PrintImage(fullCommand);
    }
    
    void PrintImage(string _cmd)
    {
        try
        {
            Process myProcess = new Process();
            //myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "cmd.exe";
            myProcess.StartInfo.Arguments = "/c " + _cmd;
            myProcess.EnableRaisingEvents = true;
            myProcess.Start();
            myProcess.WaitForExit();
        }
        catch (Exception e)
        {
            UnityEngine.Debug.Log(e);
        }
    }
    

    【讨论】:

    • 谢谢,也许它对我在此期间尝试的任何人都有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多