Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...