简体   繁体   中英

How to direct printing of photo or text using Unity without preview

I am the beginner on unity, I want to print an image directly to connected default printer without preview.

I am using this code for print but it takes the preview

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(); 

  }
 }

That's was not duplicate because this was not a window tag question I am asking about C# unity the question which tagged duplicate was not working with C# Unity. I am solved by LSPrinter Simple from assest store of unity.

I solve by using Ls Printer

If printerName is empty or null, it will print to your default printer.

Currently, it works on Windows. Every printer should work.

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

it takes a small preview

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

May be Im late but following code seems to be working for me in 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM